0

Здравствуйте!

Я скачиваю архив изображений с интернета и архивирую:

recipe.imageRecipe = NSKeyedArchiver.archivedData(withRootObject: CDataArray) as NSData

, где CDataArray имеет тип NSMutableArray. После чего отображаю это все на главном экране:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MainTableViewCell

    let myRecipe = recipe[indexPath.row]

    let img = NSKeyedUnarchiver.unarchiveObject(with: myRecipe.imageRecipe! as Data) as! NSMutableArray

    cell.nameRecipe.text = myRecipe.nameRecipe
    cell.imageRecipe.image = UIImage(data: img[0] as! Data)


    return cell
}

, но потом у меня есть окно удаления фото:

var newImages: [NSData] = []

override func viewDidLoad() {
    super.viewDidLoad()

    let images = NSKeyedUnarchiver.unarchiveObject(with: self.recipe.imageRecipe! as Data)

    newImages = images as! [NSData]

и сама перезапись,

   @IBAction func saveEditRecipe() {

    let CDataArray = newImages as! NSMutableArray

    if let recipe = recipe {

        if recipe.value(forKey: "nameRecipe") != nil {
            recipe.setValue(textEditName.text, forKey: "nameRecipe")
        }

        if recipe.value(forKey: "descriptionRecipe") != nil {
            recipe.setValue(textEditDescription.text, forKey: "descriptionRecipe")
        }

        if recipe.value(forKey: "imageRecipe") != nil {
            recipe.setValue(NSKeyedArchiver.archivedData(withRootObject: CDataArray), forKey: "imageRecipe")
        }

    } else {
        print("unable to fetch or create recipe")
    }


    (UIApplication.shared.delegate as! AppDelegate).saveContext()

    self.goToListOfRecepies()

}

, после чего у меня вылезает ошибка, та что в шапке, в этом моменте:

let img = NSKeyedUnarchiver.unarchiveObject(with: myRecipe.imageRecipe! as Data) as! NSMutableArray

Что ему не нравится, если я делаю идентичные операции?

1 ответ 1

0

NSKeyedUnarchiver возвращает immutable object

let img = NSKeyedUnarchiver.unarchiveObject(with: myRecipe.imageRecipe! as Data) as! NSArray

И используйте "Array" swift-а, это упростит жизнь.

let someArray = Array()  //immutable
var someArray = Array()  //mutable
1
  • Исправил с NSMutableArray на NSArray и все стало отлично) Благодарю что уделили время и разобрались в коде) 26 июн 2017 в 18:55

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.