Swift String length property, swiftproperty

Source: Internet
Author: User

Swift String length property, swiftproperty

Swift's String does not have the length attribute, so it is uncomfortable. Every time you want to obtain the String length, you must use the global function countElements.


No way. You only need to extend the String struct and add an attribute to it.


import Foundationextension String {        // readonly computed property    var length: Int {        return countElements(self)    }}

        let a = "hechengmen"                println(a.length)    // 10

However, unlike Objective-C, swift uses extension instead of category and has no name. In addition, extension can only add computed property, but cannot add stored property or property observer.


C ++ stringlength

You have mixed up the basic facts.
The string in your code is not the same as the String you found on MSDN.

If you look at it carefully, you should find that the string header letters you write are in lowercase. The first String letter you find on MSDN is in uppercase, and C ++ is case sensitive, you see, they are not one thing at all.

On MSDN, you can find the String under. net Framework, which belongs to the C ++ hosting system. To put it simply, this is not the standard C ++. The standard C ++ syntax does not have the concept of "attribute" or the property keyword! This is Microsoft's syntax to allow C ++ to work with its. net extension. You cannot compile in other C ++ compilers except VC.

The string used in your code is the std: string contained in the Standard C ++ library. This string has a length () method (in C ++, the "method" is called a "member function"), so you must use it with parentheses, because there is no such thing as property in Standard C ++!
If you want to check the standard string introduction on MSDN, you can enter basic_string in the index, and then you can find the std: string prototype, you can see how different it is from your hosted String.

If you really want to learn C ++, you should not mix the. net set together. This will mislead you a lot about C ++ learning. If you want to learn. net, it is better to learn C #. To be honest, the Hosted version of C ++ by Microsoft is a little different. No one is compatible with it.

PS: In addition, struct in C ++ can also contain methods (that is, member functions), which is different from C.

Systemgetproperty (String key, String value) Hello, buddy, you can set the same key to the system variable in this method.

No. The responsible person will report an error.
Sun Cheng [authoritative expert]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.