2

I don't know how to use variables when creating Instances or adressing them in Swift: For exmaple how do I do following in a loop (creating Instances):

class Guest {
    let name: String
    var age: Int 
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

let guests = [["ann", 1] , ["bob", 2] ...]

so that the loop equals :

let ann = Guest(name: "ann" , age: 1)
let bob = Guest(name: "bob" , age: 2)
...

edit: I am looking for something like this:

for i in guests {
  let i[0] = Guest(name: i[0] , age: i[1])   

Example for adressing:

print(guests[0].age)
>>>1

I've searched a lot but am getting directed to issues regarding creating variables in classes.

Thank you very much!

4
  • Your question is very vague, please try to edit it so we can help you. Commented Aug 31, 2017 at 20:35
  • The loop you posted should work. Have you tried it? Commented Aug 31, 2017 at 20:40
  • @PEEJWEEJ yes I've tried, swift in terminal raises following error: repl.swift:11:10: error: consecutive statements on a line must be separated by ';' Where the "mistake pointer" points to the "i[0]" in let i[0] = .... Commented Aug 31, 2017 at 20:47
  • Oh for one. you're mutating your guests array which you can't do. You need to set it to a new array. See Charles Srstka's anwer Commented Aug 31, 2017 at 20:51

2 Answers 2

2

You can do that with a classic loop:

let input = [("Ann", 1), ("Bob", 2)]

var guests: [Guest] = []
for each in input {
    guests.append(Guest(name: each.0, age: each.1))
}

However, it can be done more concisely (and with avoidance of var) using functional techniques:

let guests = [("Ann", 1), ("Bob", 2)].map { Guest(name: $0.0, age: $0.1) }

EDIT: Dictionary-based solution (Swift 4; for Swift 3 version just use the classic loop)

let input = [("Ann", 1), ("Bob", 2)]
let guests = Dictionary(uniqueKeysWithValues: input.map {
    ($0.0, Guest(name: $0.0, age: $0.1))
})

Or, if it's possible for two guests to have the same name:

let guests = Dictionary(input.map { ($0.0, Guest(name: $0.0, age: $0.1)) }) { first, second in
    // put code here to choose which of two conflicting guests to return
    return first
}

With the dictionary, you can just do:

if let annsAge = guests["Ann"]?.age {
    // do something with the value
}
Sign up to request clarification or add additional context in comments.

8 Comments

I would ditch the foreach loop altogether
I wanted to demonstrate the increased conciseness of the functional approach via the contrast.
Close :) - but i still don't have Ann.age
Huh? Yes, you do, with both of them.
print(Ann.age) gives me "error: use of unresolved identifier 'Ann' " I can only do print(guests[0].age) -or?
|
0
   //MARK: Call method to create multiple instances 

createInstance([("Ann", 1), ("Bob", 2)])

func createInstance(_ input: Array<Guest>) {        
    for each in input {
        guests.append(Guest(name: each.0, age: each.1))
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.