Justin Zhao
Justin Zhao

Reputation: 37

How to return an array?

I am trying to return an array with all the variables. So I can store everything inside an array. How can I fix my code so that it puts everything into an array?

func getFriendRecord () -> [String] {
    var info = ""
    let fetchRequest: NSFetchRequest<Friend> = Friend.fetchRequest()
    do {
        let searchResults = try getContext().fetch(fetchRequest)
        for trans in searchResults as [NSManagedObject] {
            let firstName = String(trans.value(forKey: "firstName") as! String)
            let lastName = String(trans.value(forKey: "lastName") as! String)
            let gender = String(trans.value(forKey: "gender") as! String)
            let age = String(trans.value(forKey: "age") as! String)
            let address = String(trans.value(forKey: "address") as! String)
            info = info + firstName + ", " + lastName + ", " + gender + ", " + age + ", " + address + "\n"
        }
    } catch {
        print("Error with request: \(error)")
    }
    return info
}

Upvotes: 0

Views: 78

Answers (2)

Nick
Nick

Reputation: 1444

You should define a struct and then you could return it and append in an array.

    struct Info {
      var firstName: String?
      var lastName: String?
      var gender: String?
      var age: String?,
      var adress: String?
    }

And then you can use this struct.

    var info  : [Info] = []
    func getFriendRecord () {
        var info = ""
        let fetchRequest: NSFetchRequest<Friend> = Friend.fetchRequest()
        do {
            let searchResults = try getContext().fetch(fetchRequest)
            for trans in searchResults as [NSManagedObject] {
                let firstName = trans.value(forKey: "firstName") as! String
                let lastName = trans.value(forKey: "lastName") as! String
                let gender = trans.value(forKey: "gender") as! String
                let age = trans.value(forKey: "age") as! String
                let address = trans.value(forKey: "address") as! String
                let information = Info(firstName:firstName,lastName:lastName,gender:gender,age:age,adress:adress)
                self.info.append(information)

            }
        } catch {
            print("Error with request: \(error)")
        }
    }

Upvotes: 0

Rob Napier
Rob Napier

Reputation: 299623

If you want to return an array, then info should be an array:

var info: [String] = []

And then you'll add additional elements to that array:

let record = [firstName, lastName, gender, age, address].joined(separator: ", ") + "\n"
info.append(record)

I've changed this to a joined because you should avoid having multiple + in an expression. In Swift, for non-obvious reasons related to overloads, it is extremely slow to compile. There's nothing wrong with it, it just doesn't work well. You could also just use string interpolation here, and that would be completely fine (possibly even a little better):

let record = "\(firstName), \(lastName), \(gender), \(age), \(address)\n"

Upvotes: 3

Related Questions