2017-10-21 3 views
-1

여러 가지 가져 오기 "Bool 유형의 값을 예상되는 인수 유형 문자열로 변환 할 수 없습니다."오류. 인코딩을위한 메소드는 문자열을 기대하지만 Bool을 얻고 있습니까?왜 Bool 유형의 값을 예상 인수 유형 문자열으로 변환 할 수 없습니까?

다음은 코드입니다. 오류에 대해서는 첨부 된 이미지를 참조하십시오.

import Foundation 

class Restaurant { 
    var name = "" 
    var item = "" 
    var location = "" 
    var image = "" 
    var isVisited = false 
    var phone = "" 
    var rating = "" 

    init(name: String, item: String, location: String, phone: String, image: String, isVisited: Bool) { 
     self.name = name 
     self.item = item 
     self.location = location 
     self.phone = phone 
     self.image = image 
     self.isVisited = isVisited 
    } 

    class func makeNewsItem(_ notificationDictionary: [String: AnyObject]) -> Restaurant? { 
     if let name = notificationDictionary["name"] as? String, 
      let phone = notificationDictionary["phone"] as? String, 
      let location = notificationDictionary["location"] as? String { 
      let date = Date() 
      let image = "" 
      let visited = false 
      let item = "" 

      let newsItem = Restaurant(name: name, item: item, location: location, phone: phone, image: image, isVisited: visited) 

      NotificationCenter.default.post(name: Notification.Name(rawValue: RestaurantTableViewController.RefreshNewsFeedNotification), object: self) 
      return newsItem 
     } 
     return nil 
    } 
} 

extension Restaurant: NSCoding { 
    struct CodingKeys { 
     static var Name = "name" 
     static var Item = "item" 
     static var Location = "location" 
     static var Image = "image" 
     static var IsVisited:Bool = false 
     static var Phone = "phone" 
     static var Rating = "rating" 
    } 

    convenience init?(coder aDecoder: NSCoder) { 
     if let name = aDecoder.decodeObject(forKey: CodingKeys.Name) as? String, 
      let location = aDecoder.decodeObject(forKey: CodingKeys.Location) as? Date, 
      let phone = aDecoder.decodeObject(forKey: CodingKeys.Phone) as? String { 

      let date = Date() 
      let image = aDecoder.decodeObject(forKey: CodingKeys.Image) as? String 
      let visited:Bool = aDecoder.decodeBool(forKey: CodingKeys.IsVisited) as? String 
      let item = aDecoder.decodeObject(forKey: CodingKeys.Item) as? String 

      self.init(name: name, item: item, location: location, phone: phone, image: image, isVisited: visited) 
     } else { 
      return nil 
     } 
    } 

    func encode(with aCoder: NSCoder) { 
     aCoder.encode(name, forKey: CodingKeys.Name) 
     aCoder.encode(location, forKey: CodingKeys.Location) 
     aCoder.encode(phone, forKey: CodingKeys.Phone) 
     aCoder.encode(item, forKey: CodingKeys.Item) 
     aCoder.encode(image, forKey: CodingKeys.Image) 
     aCoder.encode(isVisited, forKey: CodingKeys.IsVisited) 
     aCoder.encode(rating, forKey: CodingKeys.Rating) 
    } 
} 

enter image description here

답변

1

당신은 forKey에 부울 값을 추가 할수 없어.

aCoder.encode(isVisited, forKey: CodingKeys.IsVisited) 

하려면 :

aCoder.encode(isVisited, forKey: "IsVisited") 

같은 경우 :

let visited:Bool = aDecoder.decodeBool(forKey: CodingKeys.IsVisited) as? String 

사람 :

let visited:Bool = aDecoder.decodeBool(forKey: "IsVisited") // note, no need for as? String here 
이 문자열 값, 그래서에서 변경해야
관련 문제