2017-04-19 5 views
0

CS193 스탠포드 코스를 완료하고 코어 데이터를 사용하여 Twitter 클라이언트의 일부로 트윗을 저장하고 있습니다.핵심 데이터를 사용하여 엔티티의 수를 늘립니다. (Twitter CS193p)

그러나 이미 존재하는 hashmention을 찾았을 때 내가 갖고있는 일치 개수를 나타내는 hash.count를 증가 시키려고했으나 hash.count가 일치하는 hashment 수와 상관없이 0 또는 2 만 저장합니다 (예 : 속성이 엔티티의 영구 저장소로 작동하지 않습니다).

class HashMention: NSManagedObject { 
static func findOrCreateHashMention(matching twitterInfo: Twitter.Mention, in context: NSManagedObjectContext) throws -> HashMention 
{ 
    let hash = HashMention (context: context) 
    let request : NSFetchRequest<HashMention> = HashMention.fetchRequest() 
    request.predicate = NSPredicate(format: "text =[cd] %@", twitterInfo.keyword) 
    do { 
     let matches = try context.fetch(request) 
     if matches.count > 0 { 
      //inc count 
      hash.count = Int32(Int(matches.count) + 1) 

      return hash 
     } 
     else{ 
      hash.count = 0 
      print("zero hash:", twitterInfo.keyword) 
      hash.text = twitterInfo.keyword.lowercased() 
      return hash 
     } 
    } 
    catch{ 
     //makes this function throw 
     throw error 
    } 
} 
} 
+0

어떤 시점에서 상황에 따라 저장 하시겠습니까? – ghostatron

+0

아니요 언제 할 수 있을까요? – stevenpcurtis

+0

전반적인 시나리오에 약간의 차이가 있지만 if/else 블록 바로 뒤에 하나의 옵션이있을 수 있습니다 ("do"절을 떠나기 직전). – ghostatron

답변

0

따라서 변경해야하는 내용과 일치하지만 위의 예에서는 배열로되어 있습니다. 따라서 대답은 다음과 같습니다 :

do { 
     let matches = try context.fetch(request) 
let mention = matches.first 
     if matches.count > 0 { 
      mention?.count = (mention?.count)! + 1 
//.. more code 
관련 문제