Uitextfield does not take the text value of a Swift Array

Asked

Viewed 203 times

0

Guys, I’m using core data to save user input to an array. Until then the code works perfectly, the problem is when to take some element of this array and put in the Uitextfield. The code doesn’t show errors, it just doesn’t work as it should.

@IBOutlet weak var quoteTextField: UILabel!
@IBOutlet weak var addQuote: UIButton!
@IBOutlet weak var deleteQuote: UIButton!

var bookArray: Array<AnyObject> = []

override func viewDidLoad() {
    super.viewDidLoad()        

    //Pega todas as quotes guardadas no CoreData
    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!
    var request = NSFetchRequest(entityName: "BookArray")
    request.returnsObjectsAsFaults = false
    bookArray = context.executeFetchRequest(request, error: nil)!

    if (bookArray.count != 0){
        var randomIndex = Int(arc4random_uniform(UInt32(bookArray.count)))
        quoteTextField.text = bookArray[randomIndex] as? String

    }
}
  • Skal, if you put a print(bookArray[randomIndex]) it returns you what? see if there is a difference between String and Int&#Xa returns;I had to write here because I can’t comment

  • Is your entity called Bookarray? If so, it is a complex object, and must have a String attribute, hence the error. Trying the cast (as?) of a complex object for a string will return null. Try bookArray[randomIndex]. nameSeuAtribute? String

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.