PFFile符合NSCoding

问题描述 投票:0回答:1

我需要在PFObject的帮助下存储我制作的UserDefaults的子类的一些实例。事实是,它在尝试对PFFile进行编码(和解码)时会崩溃。

我查了一下,发现为了存档PFFile,它也应该符合NSCoding协议,但显然不符合。因此,我将这些文件添加到了项目https://github.com/eladb/Parse-NSCoding

现在一切正常,除了我打电话给retrieveAdsFromDevice()时。然后我得到这个崩溃:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[PFFile initWithCoder:]: unrecognized selector sent to instance 0x600000c386f0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e0051bb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x000000010d5ab735 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e023f44 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x000000010e009ed6 ___forwarding___ + 1446
    4   CoreFoundation                      0x000000010e00bda8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00000001099c5685 _decodeObjectBinary + 2409
    6   Foundation                          0x00000001099c3c67 _decodeObject + 246
    7   Foundation                          0x00000001099c3b63 -[NSKeyedUnarchiver decodeObjectForKey:] + 205
    8   NUP                                 0x000000010626f768 $S3NUP4NPAdC5coderACSgSo7NSCoderC_tcfc + 3576
    9   NUP                                 0x000000010627015f $S3NUP4NPAdC5coderACSgSo7NSCoderC_tcfcTo + 47
    10  Foundation                          0x00000001099c5685 _decodeObjectBinary + 2409
    11  Foundation                          0x00000001099c485f -[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:] + 1684
    12  Foundation                          0x0000000109958c1c -[NSArray(NSArray) initWithCoder:] + 198
    13  Foundation                          0x00000001099c5685 _decodeObjectBinary + 2409
    14  Foundation                          0x00000001099c3c67 _decodeObject + 246
    15  Foundation                          0x00000001099c3b63 -[NSKeyedUnarchiver decodeObjectForKey:] + 205
    16  Foundation                          0x00000001099c2e64 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 84
    17  NUP                                 0x00000001061d9b87 $S3NUP11NPAdManagerC21retrieveAdsFromDeviceyyF + 887
    18  NUP                                 0x00000001062d2a26 $S3NUP18HomeViewControllerC11viewDidLoadyyF + 438
    19  NUP                                 0x00000001062d2c44 $S3NUP18HomeViewControllerC11viewDidLoadyyFTo + 36
    20  UIKitCore                           0x0000000116b3f4e1 -[UIViewController loadViewIfRequired] + 1186
    21  UIKitCore                           0x0000000116b3f940 -[UIViewController view] + 27
    22  NUP                                 0x00000001063349c5 $S3NUP22RootPageViewControllerC21viewDidLayoutSubviewsyyF + 7237
    23  NUP                                 0x0000000106336444 $S3NUP22RootPageViewControllerC21viewDidLayoutSubviewsyyFTo + 36
    24  UIKitCore                           0x0000000117648914 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1824
    25  QuartzCore                          0x000000010af8ab19 -[CALayer layoutSublayers] + 175
    26  QuartzCore                          0x000000010af8f9d3 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 395
    27  QuartzCore                          0x000000010af087ca _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 342
    28  QuartzCore                          0x000000010af3f97e _ZN2CA11Transaction6commitEv + 576
    29  UIKitCore                           0x00000001171792d0 __34-[UIApplication _firstCommitBlock]_block_invoke_2 + 139
    30  CoreFoundation                      0x000000010df6a62c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    31  CoreFoundation                      0x000000010df69de0 __CFRunLoopDoBlocks + 336
    32  CoreFoundation                      0x000000010df64654 __CFRunLoopRun + 1284
    33  CoreFoundation                      0x000000010df63e11 CFRunLoopRunSpecific + 625
    34  GraphicsServices                    0x00000001109251dd GSEventRunModal + 62
    35  UIKitCore                           0x000000011715e81d UIApplicationMain + 140
    36  NUP                                 0x00000001063b8a44 main + 68
    37  libdyld.dylib                       0x000000010ee27575 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我的课程:

class NPAd: PFObject, NSCoding {
    @NSManaged var type: String
    @NSManaged var imageFile: PFFile
    @NSManaged var link: String
    @NSManaged var brandName: String
    @NSManaged var brandIcon: PFFile

    override init() {
        super.init()
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(type, forKey: "type")
        aCoder.encode(imageFile, forKey: "imageFile")
        aCoder.encode(link, forKey: "link")
        aCoder.encode(brandName, forKey: "brandName")
        aCoder.encode(brandIcon, forKey: "brandIcon")
    }

    required init?(coder aDecoder: NSCoder) {
        super.init()

        type = aDecoder.decodeObject(forKey: "type") as! String
        imageFile = aDecoder.decodeObject(forKey: "imageFile") as! PFFile
        link = aDecoder.decodeObject(forKey: "link") as! String
        brandName = aDecoder.decodeObject(forKey: "brandName") as! String
        brandIcon = aDecoder.decodeObject(forKey: "brandIcon") as! PFFile
    }
}

这就是我存储和检索数据的方式:

func storeAdsToDevice(_ adsToView: [NPAd]) {
    let archivedData = NSKeyedArchiver.archivedData(withRootObject: adsToView)
    UserDefaults.standard.set(archivedData, forKey: "AdsToView")
}

func retrieveAdsFromDevice() -> [NPAd] {
    let archivedData = UserDefaults.standard.object(forKey: "AdsToView")

    if let archivedData = archivedData as? Data {
        return NSKeyedUnarchiver.unarchiveObject(with: archivedData) as? [NPAd] ?? []
    }

    return []
}

有什么想法吗?

我需要在UserDefaults的帮助下存储我制作的PFObject子类的某些实例。事实是,它在尝试编码(和解码)PFFile时会崩溃。我查了一下,发现...

swift parse-platform nscoding
1个回答
0
投票

我尝试安装您的Pod,但似乎缺少文件:

© www.soinside.com 2019 - 2024. All rights reserved.