0

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
}
3
  • Returning an array is a bad idea. You should define a struct with appropriate fields and return that. Commented Sep 23, 2019 at 0:41
  • Unrelated but there's little sense in combing the use of String( ... ) with as! String. A line such as let firstName = trans.value(forKey: "firstName") as! String is enough. Commented Sep 23, 2019 at 0:49
  • You need to create one array of string and add "info" (String) to that array, because your function return type is [String]. Commented Sep 23, 2019 at 4:51

2 Answers 2

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Dude you are a god. I fixed it using your thing. OMG you are the smartest person in the world. Thank you so much! I love you!
0

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)")
        }
    }

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.