Discussion regarding Usage of some New Keywords in Swift and How to use in dat to day coding and best way to use this keywords.
Welcome to the new discussion related to Swift Language!
Here we will discuss some new keywords introduce in Swift and how to use in day to day coding.
Some Keywords which I found while doing code
-
Final (Class & Variables).
-
FilePrivate ( Function & Variables).
-
guard.
-
BOOL in Swift 3.0.
-
_ Usage in Swift.
-
Private & Global Variables.
-
? usage in Swift.
Hello Guys, I just write the code and let me know, that code will be compile or not? If not then give the reason for that.
let scores = [45,2,5,6]
var totalScore = 1
for score in scores {
score *= totalScore
}
Use of ?? Operator in Swift.
Another way to handle optional values is to provide a default value using the ?? operator. If the optional value is missing, the default value is used instead.
Example:
let nikName: String? = nil
let fullName: String = "John"
let greeting = "Hi \(nikName ?? fullName)"
Output: Hi John
let nikName: String? = "Marsh"
let fullName: String = "John"
let greeting = "Hi \(nikName ?? fullName)"
Output: Hi Marsh
Above code will not compile as above line changing let type variable and I think by mistake you have put this line.
You can use operator to manipulate value like below:
for score in scores {
totalScore += score
}
Output : 59
for score in scores {
totalScore *= score
}
Output : 2700
Correct me if I misunderstood your question ?
Thanks.
Difference Between weak
& unowned
in respect to ARC memory management?
One more keyword with new feature
lazy :
lazy var completePhoneNumber: () -> String = {
self.countryCode + " " + self.number
}
Above closure bind countrycode & phone number and returns a complete phone number.
The property is declared with lazy, meaning that it will not be assigned until it’s used the first time.
This is required because it’s using self.countryCode
and self.number,
which aren’t available until after the initializer runs.
Why we are ending any variable declaration with ! symbol ?
@IBOutlet var wkTableView: WKInterfaceTable!
var selectedRow : NSInteger!
Please provide answer with valid example.
“?” and “!” Both are represented nil value. But the main difference is that if we declare the variable with “!” then at the time of the access of variable we don’t need to unwrap.
Example:
var selectedRow: NSInteger!
var selectedRowOptional: NSInteger?
//Assign the value
selectedRow = 2
selectedRowOptional = 2
print("Value is \(selectedRow)")
OutPut: Value is 2
print("Value is \(selectedRowOptional)")
OutPut: Value is Optional(2)
In Swift 2.0 below @IBOutlet
init supported
@IBOutlet var pacificLabel : WKInterfaceLabel? = WKInterfaceLabel()
But in Swift 3.0 its deprecated
@IBOutlet var pacificLabel : WKInterfaceLabel?
This one only supported and reason behind this why apple removed default init from Outlet ?
Why we assign a value of IBOutlet variable at the time the initialization? We already bound with Storyboard.
Ok In Case of StoryBoard bounding its correct but what about manually adding some controls based on service response ? will it allowed to initialized through delecration ?
With IBOutlet It’s not possible
Without IBOutlet
also its not allowed
like
var pacificLabel : WKInterfaceLabel? = WKInterfaceLabel()
Got same error.
any comment on this ?
Yes, You Got Error. Because You declare a variable with optional and same time you create a label and assign.
So what would be the best practice for above scenario ?
You can declare variable without optional type.
var pacificLabel = WKInterfaceLabel()
Write the code in Swift, Print the value like “-5, -4, -3 , -2, -1, 0” Only use of For In.
Note: No use of any if else statement in code. Just use of For In.
I noticed the guard keyword in your keywords list you found while coding. Please read my tutorial about guard versus if when you actually get a chance and let me know if you have any questions or issues of any kind whatsoever about the whole thing afterwards: