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