2

I am using core data to save user input in an array. Until then the code works perfectly , the problem is the time to get some element of that array and put in UITextField as a text. The code has no errors , just does not 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()        

//Get the info on the 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
   }
}

DataModel

And this is where I am saving the user input, which is located in another View Controller:

 @IBAction func saveOnData(sender: AnyObject) {

    var quoteInputText = quoteInput.text

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!

    var newQuote = NSEntityDescription.insertNewObjectForEntityForName("BookArray", inManagedObjectContext: context) as! NSManagedObject

    newQuote.setValue(quoteInputText, forKey: "myFirstBook")

    context.save(nil)

    println(newQuote)

}
1
  • Can you please post the declaration of your BookArray entity? I will write down what's wrong with this code, but if you provide the declaration I will show you how to fix it :) Commented Jul 14, 2015 at 20:41

1 Answer 1

1

Going line by line.

First you create a fetch request:

var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var request = NSFetchRequest(entityName: "BookArray")
request.returnsObjectsAsFaults = false

Then you assign the result of your fetch request to the array called bookArray:

bookArray = context.executeFetchRequest(request, error: nil)!

Function executeFetchRequest returns [AnyObject]. In your case it will return an array of BookArray entities [BookArray].

So, when you call:

bookArray[randomIndex]

you get an instance of BookArray. Of course your conditional downcast to String will always fail ;)

Here is what you have to do:

1) Add the following class to your project:

import Foundation
import CoreData

@objc(BookArray)
class BookArray:NSManagedObject {
    @NSManaged var myFirstBook:String
}

Then open you Core Data model, click on the BookArray entity and make sure that you et its class to BookArray in the attributes inspector:

enter image description here

2) With such implementation you can rewrite your save function to:

 @IBAction func saveOnData(sender: AnyObject) {

    var quoteInputText = quoteInput.text

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!

    var newQuote = NSEntityDescription.insertNewObjectForEntityForName("BookArray", inManagedObjectContext: context) as! BookArray

    newQuote.myFirstBook = quoteInputText

    context.save(nil)

    println(newQuote)

}

3) Change the unwrapping part of your viewDidLoad function to:

if (bookArray.count != 0){
    var randomIndex = Int(arc4random_uniform(UInt32(bookArray.count)))
    let bookArrayInstance = bookArray[randomIndex] as! BookArray
    quoteTextField.text = bookArrayInstance.myFirstBook
   }
}
Sign up to request clarification or add additional context in comments.

15 Comments

I tried what you suggested it didn't work. BookArray is an entity on the core data
You should call bookArrayInstance.myFirstBook. This is the string property which you have.
Yes, but he is complaining here: let bookArrayInstance = bookArray[randomIndex] as! BookArray as Use of undeclared type "BookArray"
So you didn't define a class for your BookArray?
How do you the save your user input? You did mention that it works
|

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.