2017-02-08 12 views
1

私は自分のデータをコアデータデータベースからフェッチし、オブジェクトに入れて、次にコールバックする配列に入れようとしています。私はgetCoffeeBrandsFromDBを実行するとSwift 3 - Core Data - オブジェクトを作成する

func getCoffeeBrandsFromDB(callback: @escaping (_ dbCoffeeBrands: Array<Any>)->()) { 
    //create a fetch request, telling it about the entity 
    print("running getCBFDB") 

    var coffeeBrandArray = Array<Any>() 

    let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "CoffeeBrand") 

    do { 

     let searchResults = try getContext().fetch(fetchRequest) 


     print ("num of results = \(searchResults.count)") 


     for brands in searchResults as! [NSManagedObject] { 


      let brand = CoffeeBrand() 
      brand.dataBaseId = brands.value(forKey: "dataBaseId") as! Int64 
      brand.brandName = brands.value(forKey: "brandName") as? String 
      brand.numberOfCoffeesNeeded = brands.value(forKey: "numberOfCoffeesNeeded") as! Int32 
      coffeeBrandArray.append(brand) 
      print("brands virker med \(brand.brandName!)") 
     } 
     print("CcffeeBrandArray.count is: \(coffeeBrandArray.count)") 
     callback(coffeeBrandArray) 
    } catch { 
     print("Error with request: \(error)") 
    } 
} 

私はこのエラーログを取得する:

running getCBFDB 
num of results = 4 
2017-02-08 15:47:02.179211 Keebin_development_1[8373:626945] [error] error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'CoffeeBrand' 
CoreData: error: CoreData: error: Failed to call designated initializer on NSManagedObject class 'CoffeeBrand' 

2017-02-08 15:47:02.180 Keebin_development_1[8373:626945] -[CoffeeBrand setDataBaseId:]: unrecognized selector sent to instance 0x608000279e80 
2017-02-08 15:47:02.189 Keebin_development_1[8373:626945] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CoffeeBrand setDataBaseId:]: unrecognized selector sent to instance 0x608000279e80' 
    *** First throw call stack: 
(
    0 CoreFoundation      0x0000000110c36d4b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x000000011027721e objc_exception_throw + 48 
    2 CoreFoundation      0x0000000110ca6f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 
    3 CoreFoundation      0x0000000110bbc005 ___forwarding___ + 1013 
    4 CoreFoundation      0x0000000110bbbb88 _CF_forwarding_prep_0 + 120 
    5 Keebin_development_1    0x000000010fc20e5c _TF20Keebin_development_121getCoffeeBrandsFromDBFT8callbackFGSaP__T__T_ + 1804 
    6 Keebin_development_1    0x000000010fc2d314 _TFC20Keebin_development_126LoyaltyCardsViewController11viewDidLoadfT_T_ + 180 
    7 Keebin_development_1    0x000000010fc2d5e2 _TToFC20Keebin_development_126LoyaltyCardsViewController11viewDidLoadfT_T_ + 34 
    8 UIKit        0x00000001111fca3d -[UIViewController loadViewIfRequired] + 1258 
    9 UIKit        0x000000011123d28f -[UINavigationController _layoutViewController:] + 55 
    10 UIKit        0x000000011123db77 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 471 
    11 UIKit        0x000000011123dcee -[UINavigationController _startTransition:fromViewController:toViewController:] + 133 
    12 UIKit        0x000000011123eef9 -[UINavigationController _startDeferredTransitionIfNeeded:] + 874 
    13 UIKit        0x000000011123ffdb -[UINavigationController __viewWillLayoutSubviews] + 58 
    14 UIKit        0x0000000111436dd7 -[UILayoutContainerView layoutSubviews] + 223 
    15 UIKit        0x000000011111fab8 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1237 
    16 QuartzCore       0x00000001161ffbf8 -[CALayer layoutSublayers] + 146 
    17 QuartzCore       0x00000001161f3440 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 
    18 QuartzCore       0x00000001161f32be _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 
    19 QuartzCore       0x0000000116181318 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 280 
    20 QuartzCore       0x00000001161ae3ff _ZN2CA11Transaction6commitEv + 475 
    21 UIKit        0x0000000111053d9b _UIApplicationFlushRunLoopCATransactionIfTooLate + 206 
    22 UIKit        0x000000011185e77c __handleEventQueue + 5672 
    23 CoreFoundation      0x0000000110bdb761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    24 CoreFoundation      0x0000000110bc098c __CFRunLoopDoSources0 + 556 
    25 CoreFoundation      0x0000000110bbfe76 __CFRunLoopRun + 918 
    26 CoreFoundation      0x0000000110bbf884 CFRunLoopRunSpecific + 420 
    27 GraphicsServices     0x00000001152f9a6f GSEventRunModal + 161 
    28 UIKit        0x000000011105ac68 UIApplicationMain + 159 
    29 Keebin_development_1    0x000000010fc3fe5f main + 111 
    30 libdyld.dylib      0x000000011434d68d start + 1 
    31 ???         0x0000000000000001 0x0 + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 

私は私の問題がどこにあるかを見つけ出すように見えることはできません。私は自分のCoffeeBrand + CoreDataClass.SwiftとCoffeeBrand + CoreDataProperties.Swiftをエディタで作成しました.-> NSManaged Object Subclassを作成し、他のSOの質問に答えました。言及

二つのクラス:

import Foundation 
import CoreData 

@objc(CoffeeBrand) 
public class CoffeeBrand: NSManagedObject { 

} 

と:

import Foundation 
import CoreData 


extension CoffeeBrand { 

    @nonobjc public class func fetchRequest() -> NSFetchRequest<CoffeeBrand> { 
     return NSFetchRequest<CoffeeBrand>(entityName: "CoffeeBrand"); 
    } 

    @NSManaged public var brandName: String? 
    @NSManaged public var dataBaseId: Int64 
    @NSManaged public var id: Int64 
    @NSManaged public var numberOfCoffeesNeeded: Int32 

} 

誰かが私が間違っているつもりだ場所を確認することはできますか?明らかにそれは私のfor-loopにありますが、私はどこで/何を/どのように正しく行うのか分かりません。

ありがとうございます!

答えて

0

どこが間違っているのか分かりました。

は、私はそれが私がDBからのフェッチのオブジェクトである

let fetchRequest: NSFetchRequest<NSManagedObject> = NSFetchRequest(entityName: "CoffeeBrand") 

このようにlet fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "CoffeeBrand") を変更しました。

それは簡単でした。私のforループは単純に次のようになります:

 for brands in searchResults { 

     coffeeBrandArray.append(brands) 

     } 
     callback(coffeeBrandArray) 
    } catch { 
     print("Error with request: \(error)") 
    } 
} 

そして今はすべてが正常に動作します。

関連する問題