Swift! And? of learning

Source: Internet
Author: User

Just started to learn these two symbols are not sure whether it is possible to replace each other with the confusion of today to do a good summary

If you declare a variable as follows

var name:string? Only declares no initialization assignment stating that the current name is nil

This indicates that name is an optional type, and name may be a string or nil ( this nil is different from the nil in OC, not a pointer, but a value that does not exist ).

1. What about calling methods, attributes, subscript indexes, and so on before you add one? , if it is a nil value (no value exists), that is, Optional.none, will skip the subsequent operation is not executed, if there is a value, that is, optional.some may be unpacking (unwrap), and then after the value of the split to perform the subsequent operation, to ensure that the operation of the security

If you want to do something if and only if the name has a value, make a decision

If name! = nil{

Do something neccessary
}

2. If the proxy method is @optional then this is also an optional proxy method declaration and use of the time to add?

! Declare as follows

var telephonestr:string!

Use! Description Telephonestr We agreed that it must have a value in the next call assignment, you do not need to add? and! Because the TELEPHONESTR must have a value.

But if the use of the process did not follow the Convention to assign it to nil will be crash

So I advocate that if there is uncertainty to do the processing or assignment of the pre-judgment or use of the type, so that is because of their own experience or less slowly more in-service can have a more accurate understanding

So it's just my experience of learning that I will continue to enrich and improve the knowledge points that are inappropriate

There are two very good learning articles to share

Www.cocoachina.com/industry/20140605/8687.html

http://blog.csdn.net/woaifen3344/article/details/30244201

Swift! And? of learning

Related Article

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.