Comma usage in swift - swift

#IBAction func selectedGame(segue:UIStoryboardSegue) {
if let gamePickerViewController = segue.sourceViewController as? GamePickerViewController,
selectedGame = gamePickerViewController.selectedGame {
detailLabel.text = selectedGame
game = selectedGame
}
}
Hi all, i'm following a tutorial to learn something about swift. Yesterday I found this part of code but I can not find a way to understand what it means thatcomma means. Can u pls explain me?

The comma is used to combine multiple optional bindings into one statement to avoid unnecessary nesting.
From Swift 1.2,The if let construct can now unwrap multiple optionals at once, as well as include intervening boolean conditions. This lets you express conditional control flow without unnecessary nesting.More details
For example:
var foo: Int!
var bar: String!
// Swift 1.2
if let foo = foo,bar = bar {
// foo & bar have values.
} else {
}
// before Swift 1.2
if let foo = foo {
// nesting
if let bar = bar {
// foo & bar have value.
}
}
Xcode6.3 and above support Swift1.2.

Related

Swift memoizing/caching lazy variable in a struct

I drank the struct/value koolaid in Swift. And now I have an interesting problem I don't know how to solve. I have a struct which is a container, e.g.
struct Foo {
var bars:[Bar]
}
As I make edits to this, I create copies so that I can keep an undo stack. So far so good. Just like the good tutorials showed. There are some derived attributes that I use with this guy though:
struct Foo {
var bars:[Bar]
var derivedValue:Int {
...
}
}
In recent profiling, I noticed a) that the computation to compute derivedValue is kind of expensive/redundant b) not always necessary to compute in a variety of use cases.
In my classic OOP way, I would make this a memoizing/lazy variable. Basically, have it be nil until called upon, compute it once and store it, and return said result on future calls. Since I'm following a "make copies to edit" pattern, the invariant wouldn't be broken.
But I can't figure out how to apply this pattern if it is struct. I can do this:
struct Foo {
var bars:[Bar]
lazy var derivedValue:Int = self.computeDerivation()
}
which works, until the struct references that value itself, e.g.
struct Foo {
var bars:[Bar]
lazy var derivedValue:Int = self.computeDerivation()
fun anotherDerivedComputation() {
return self.derivedValue / 2
}
}
At this point, the compiler complains because anotherDerivedComputation is causing a change to the receiver and therefore needs to be marked mutating. That just feels wrong to make an accessor be marked mutating. But for grins, I try it, but that creates a new raft of problems. Now anywhere where I have an expression like
XCTAssertEqaul(foo.anotherDerivedComputation(), 20)
the compiler complains because a parameter is implicitly a non mutating let value, not a var.
Is there a pattern I'm missing for having a struct with a deferred/lazy/cached member?
Memoization doesn't happen inside the struct. The way to memoize is to store a dictionary off in some separate space. The key is whatever goes into deriving the value and the value is the value, calculated once. You could make it a static of the struct type, just as a way of namespacing it.
struct S {
static var memo = [Int:Int]()
var i : Int
var square : Int {
if let result = S.memo[i] {return result}
print("calculating")
let newresult = i*i // pretend that's expensive
S.memo[i] = newresult
return newresult
}
}
var s = S(i:2)
s.square // calculating
s = S(i:2)
s.square // [nothing]
s = S(i:3)
s.square // calculating
The only way I know to make this work is to wrap the lazy member in a class. That way, the struct containing the reference to the object can remain immutable while the object itself can be mutated.
I wrote a blog post about this topic a few years ago: Lazy Properties in Structs. It goes into a lot more detail on the specifics and suggest two different approaches for the design of the wrapper class, depending on whether the lazy member needs instance information from the struct to compute the cached value or not.
I generalized the problem to a simpler one: An x,y Point struct, that wants to lazily compute/cache the value for r(adius). I went with the ref wrapper around a block closure and came up with the following. I call it a "Once" block.
import Foundation
class Once<Input,Output> {
let block:(Input)->Output
private var cache:Output? = nil
init(_ block:#escaping (Input)->Output) {
self.block = block
}
func once(_ input:Input) -> Output {
if self.cache == nil {
self.cache = self.block(input)
}
return self.cache!
}
}
struct Point {
let x:Float
let y:Float
private let rOnce:Once<Point,Float> = Once {myself in myself.computeRadius()}
init(x:Float, y:Float) {
self.x = x
self.y = y
}
var r:Float {
return self.rOnce.once(self)
}
func computeRadius() -> Float {
return sqrtf((self.x * self.x) + (self.y * self.y))
}
}
let p = Point(x: 30, y: 40)
print("p.r \(p.r)")
I made the choice to have the OnceBlock take an input, because otherwise initializing it as a function that has a reference to self is a pain because self doesn't exist yet at initialization, so it was easier to just defer that linkage to the cache/call site (the var r:Float)

Variable 'xxx' was never mutated; in derived class

I'm posting my first message here, I've a logical question about swift language. For your information, I'm quite new in swift language, I'm use to code in c++ and it's a bit hard for me to have an objective point of view on how to do things right (in an elegant way), if you have some advices, pls feel free to do your suggestions.
I'm doing a homemade encapsulation using the following superclass :
class MultiLevel_encapsulation {
var separator = "";
var datas:[String:String] = [:]
func wrap() -> String{
var out:String = ""
var i = 0
for (key, data) in datas{
if i==0{
out += key + separator + data
}
else{
out += separator + key + separator + data
}
i+=1
}
return out
}
func unwrap(content:String){
let split = content.components(separatedBy: separator)
var i = 1
while(i < split.count){
datas[split[i-1]] = split[i]
i += 2
}
}
func getAttributesNames() -> [String]{
var out:[String] = []
for (key, _) in datas{
out.append(key)
}
return out
}
func getValue(name:String) -> String? {
return datas[name];
}
func setValue(name:String, value:String){
datas[name] = value;
}
}
and I want to create some subclasses including the superclass, I just change the separator depending of the subclass name :
class Level5_encapsulation: MultiLevel_encapsulation{
init(message:String) {
super.init()
separator = "&&LEVEL5&&"
unwrap(content:message)
}
override init() {
super.init()
separator = "&&LEVEL5&&"
}
}
So after it I just need to create the subclass as a var in my program, add values and wrap it to have an encapsulated string :
var l5message = Level5_encapsulation()
l5message.setValue(name: #anyTitle#, value: #anyValue#)
var output = l5message.wrap() // String with encapsulated message
Do you think it 's the right way to do it or is there a better way for that ?
My main question is about this compiler warning :
Variable 'l5message' was never mutated; consider changing to 'let' constant
I changed it for a let and it works.
So there is something I don't understand : Why can I change proprieties in the superclass as if the inherited subclass is declared as constant ? Where is the storage of the superclass and how does it works ?
In Swift classes and structs behave a bit differently than in C++. var and let prevent changes to the actual value, and since the variable type that you're using is a class the variable holds a reference, and not the actual data (Like Level5_encapsulation *l5message).
Since you're not mutating the value of the variable (A reference), the compiler raises a warning.

Swift Conversion using a variable

Is there anyway to use conversion using a variable? I am using object stacking using type of "AnyObject" and I've been able to take the class type and populate a variable. Now I need to populate an array using conversion.
var myString = "Hello World"
var objectStack = [AnyObject]()
objectStack.append(myString)
let currentObject = String(describing: objectStack.last!)
var objectType = String()
let range: Range<String.Index> = currentObject.range(of: ":")!
objectType = currentObject.substring(to: range.lowerBound)
let range2: Range<String.Index> = objectType.range(of: ".")!
objectType = objectType.substring(from: range2.upperBound)
The code above will evaluate the class and set the value of "objectType" to "String". Now I'm trying to go the other way. Something like this:
for obj in objectStack{
obj = newObject as! objectType //this doesn't work
}
Is something like this possible?
There is a simpler, safer way to get the type:
let type = type(of: objectStack.last!) // String.Type
let typeString = String(describing: type) // "String"
The other way around is not possible because the type of the object is not known at compile time. Do you have a number of known types you want to try to cast to? In that case, use optional binding to check if the cast is successful:
let object = objectStack.last!
if let string = object as? String {
// do String stuff
}
else if let i = object as? Int {
// do Int stuff
}
// and so on
If you have a large number of possible types that share some common functionality: Use Protocols. See Swift Documentation for a nice introduction.
You define a protocol for some common functionality that different types can implement:
protocol Stackable {
func doStuff()
// (more methods or properties if necessary)
}
The protocol provides a contract that all types conforming to this protocol have to fulfill by providing implementations for all declared methods and properties. Let's create a struct that conforms to Stackable:
struct Foo: Stackable {
func doStuff() {
print("Foo is doing stuff.")
}
}
You can also extend existing types to make them conform to a protocol. Let's make String Stackable:
extension String: Stackable {
func doStuff() {
print("'\(self)' is pretending to do stuff.")
}
}
Let's try it out:
let stack: [Stackable] = [Foo(), "Cat"]
for item in stack {
item.doStuff()
}
/*
prints the following:
Foo is doing stuff.
'Cat' is pretending to do stuff.
*/
This worked for me:
var instance: AnyObject! = nil
let classInst = NSClassFromString(objectType) as! NSObject.Type
instance = classInst.init()

Initializing variables in Swift without var/let

I'm new to Swift so this might come as a noob question but I will be thankful if someone cleared the doubt because I cannot find any explanation online. While using for-in loop, sometimes the variables have not been initialized before and still there is no compiler error. Sometimes if I try to write var/let before them, it shows error- 'let' pattern cannot appear nested in an already immutable context
e.g., in the code below, why have the variables movie and releasedDate not been initialized before?
class MovieArchive {
func filterByYear(year:Int, movies:Dictionary<String, Int> ) -> [String]{
var filteredArray = [String]()
for (movie, releaseDate) in movies {
if year == releaseDate {
filteredArray.append(movie)
}
}
return filteredArray
}
}
var aiThemedMovies = ["Metropolis": 1927, "2001: A Space Odyssey": 1968, "Blade Runner": 1982, "War Games": 1983, "Terminator": 1984, "The Matrix": 1999, "A.I.": 2001, "Her": 2013, "Ex Machina": 2015]
var myArchive = MovieArchive()
myArchive.filterByYear(year: 2013
, movies: aiThemedMovies)
Thanks in advance for any help :)
Swift does initialise movie and releaseDate using let, but simplifies that initialisation so that you can just assume that they're set to the correct variables from movies every time the loop iterates. That's why you don't need to write let before movie and releaseDate.
If you write var in front of the variable, you can mutate the value in the array that it points to. If it's a let, then it won't let you mutate it.
For example, if you place var in front of one of the variables, this code will change all the releaseDates to 2000:
class MovieArchive {
func filterByYear(year:Int, movies:Dictionary<String, Int> ) -> [String]{
var filteredArray = [String]()
for (movie, var releaseDate) in movies {
releaseDate = 2000
if year == releaseDate {
filteredArray.append(movie)
}
}
return filteredArray
}
}
Without concerning ourselves with practical usage, let's just talk about what is syntactically legal in a for loop. This is legal:
for var i in 1...3 { // ok
print(i)
i = 0
}
This is not legal:
for let i in 1...3 { // error
print(i)
}
Why? Because the let is implicit when you say this:
for i in 1...3 { // ok
print(i)
}
You didn't say var, so i is automatically declared with let. It's just a way of making your life simpler. Don't worry, be happy.
(movie, releaseDate) is a tuple of variables. Swift allows initialising multiple variables at once using tuples.
let (movie, releaseDate) = ("Metropolis", 1927)
A for-in loop is syntactic sugar to redeclare those constants for each element of your collection. In that case, let is implicit.
There are other ways to get named constants with implicit let, like using a closure:
class MovieArchive {
func filterByYear(year: Int, movies: [String: Int]) -> [String] {
return movies.filter { (movie, releaseDate) -> Bool in
year == releaseDate
}.map { $0.key }
}
}
The above example gives the same results as your code by the way.
But apart from for-in, func parameters and closures, I can't think of other cases where the let/var keyword would be omitted in Swift 3.0.

Swift subclassing, recursion, and super casting

I'm trying to define a Swift class that has a recursive function that returns the names of all the variables in the class. It works for printing it's variables, but when I use this I'll be using it as a base class for my models, and I may have multiple layers of inheritance. I want the method to return an array of all variable names on the current instance, as well as the names of any variables on any super classes, until we reach the base Cool class.
class Cool:NSObject {
func doStuff() -> [String] {
var values = [String]()
let mirrorTypes = reflect(self)
for i in 0 ..< mirrorTypes.count {
let (name, type) = mirrorTypes[i]
if let superCool = super as! Cool while name == "super" {
values += superCool.doStuff()
}
}
return values
}
}
The problem is in:
if let superCool = super as! Cool while name == "super" {
It causes an Expected '.' or '[' after super compiler error.