objective-c – 在Swift中实现NSCoding协议有什么问题

前端之家收集整理的这篇文章主要介绍了objective-c – 在Swift中实现NSCoding协议有什么问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我认为我会谨慎,并通过转换一个类来尝试在现有的Obj-C项目上使用 Swift.还有一个小而简单的.噢亲爱的.

将原始的obj-c音译为Swift应该是直截了当的,所以看起来如此.不幸的是,虽然编码器到持久存储似乎有效,但它在init编码器的第一行出现EXC_BREAKPOINT错误而崩溃.

IF(和大写是有意的)NSCoding / Swift提供与NSCoding / ObjC相同的持久内容,然后我的所有obj-c版本应该能够读取Swift编码的内容,反之亦然.事实证明并非如此 – 当我试图从Swift版本中读取持久性存储时,我完美运行的obj-c版本崩溃了.当然,如果NSCoding正确实现,它应该生成一个可读的东西吗?否则,应该有单独的NSCodingSwift和NSCodingObjC协议?

总而言之,我可以在obj-c中读/写.我不能写/ obj-c和read / swift我可以写/ swift read / obj-c而且我无法在swift中读/写.

以下是两个版本:

let keyBeaconItemNameKey = "name"
let keyBeaconItemUUIDKey = "uuid"
let keyBeaconItemMajorValueKey = "major"
let keyBeaconItemMinorValueKey = "minor"

import UIKit
import CoreLocation

class SMBeaconItem : NSObject,NSCoding
{
    var name : String!
    var uuid : NSUUID!
    var major : NSNumber!
    var minor : NSNumber!

    init(newName : String,newUUID : NSUUID,newMajor : NSNumber,newMinor : NSNumber )
    {
        name = newName
        uuid = newUUID
        major = newMajor
        minor = newMinor
    }

    init( coder decoder : NSCoder!)
    {
        name = decoder.decodeObjectForKey(keyBeaconItemNameKey) as String
        uuid = decoder.decodeObjectForKey(keyBeaconItemUUIDKey) as NSUUID
        major = decoder.decodeObjectForKey(keyBeaconItemMajorValueKey) as NSNumber
        minor = decoder.decodeObjectForKey(keyBeaconItemMinorValueKey) as NSNumber
    }

    func encodeWithCoder( encoder: NSCoder!)
    {
        encoder.encodeObject(name,forKey:keyBeaconItemNameKey)
        encoder.encodeObject(uuid,forKey:keyBeaconItemUUIDKey)
        encoder.encodeObject(major,forKey:keyBeaconItemMajorValueKey)
        encoder.encodeObject(minor,forKey:keyBeaconItemMinorValueKey)
       }
    }

工作原创:

@implementation SMBeaconItem

- (instancetype)initWithName:(NSString *)name uuid:(NSUUID *)uuid major:(CLBeaconMajorValue)major minor:(CLBeaconMinorValue)minor
{
    self = [super init];
    if (!self)
    {
        return nil;
    }

    _name = name;
    _uuid = uuid;
    _majorValue = major;
    _minorValue = minor;

    return self;
}

#pragma mark - Persistence

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (!self)
    {
        return nil;
    }

    _name = [aDecoder decodeObjectForKey:keyBeaconItemNameKey];
    _uuid = [aDecoder decodeObjectForKey:keyBeaconItemUUIDKey];
    _majorValue = [[aDecoder decodeObjectForKey:keyBeaconItemMajorValueKey] unsignedIntegerValue];
    _minorValue = [[aDecoder decodeObjectForKey:keyBeaconItemMinorValueKey] unsignedIntegerValue];

    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:keyBeaconItemNameKey];
    [aCoder encodeObject:self.uuid forKey:keyBeaconItemUUIDKey];
    [aCoder encodeObject:[NSNumber numberWithUnsignedInteger:self.majorValue] forKey:keyBeaconItemMajorValueKey];
    [aCoder encodeObject:[NSNumber numberWithUnsignedInteger:self.minorValue] forKey:keyBeaconItemMinorValueKey];
}

@end

谢谢你提供的所有帮助.

解决方法

唯一突出的是你使用String而不是NSString作为名称的类型.在Apple推出的测试版中,String(奇怪的是)不是NSString的一对一替代品.即,缺少某些方法,需要调用.bridgeToObjectiveC()来获取NSString版本.使用该类型可能会符合NSCoder的期望,尽管这种差异不应该如此.

我没有真正测试这个说法,因为我不在我的开发机器上.但这是我的直觉.尝试一下,看看会发生什么!如果没有任何变化,请尝试切换您设置变量的顺序,看看它是否与名称字段相关或仅仅是init函数的第一行.

猜你在找的C&C++相关文章