2017-04-19 12 views
0

私はCS193スタンフォードコースを修了し、コアデータを使用してTwitterクライアントの一部としてツイートを保存しています。コアデータを使用してエンティティのカウントを増やす(Twitter CS193p)

しかし、私が存在するhashmentionを見つけたら、どれくらいの一致を表すhash.countをインクリメントしたいのですが、hash.countがいくつあっても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」節を残す直前に)1つのオプションがあります。 – ghostatron

答えて

0

したがって、変更する必要がありますが、上記の例では配列になっています。したがって、答えは次のとおりです。

do { 
     let matches = try context.fetch(request) 
let mention = matches.first 
     if matches.count > 0 { 
      mention?.count = (mention?.count)! + 1 
//.. more code 
関連する問題