我的问题是我检索我的NSArray Store对象,我的所有NSString属性都导致BadAccess错误.int和double属性工作正常!
store.h
@interface Store : NSObject<NSCoding> {
NSString *Name;
NSString *Address;
NSString *Phone;
double GeoLong;
double GeoLat;
int ID;
}
@property (nonatomic, retain) NSString *Name;
@property (nonatomic, retain) NSString *Address;
@property (nonatomic, retain) NSString *Phone;
@property (nonatomic) double GeoLat;
@property (nonatomic) double GeoLong;
@property (nonatomic) int ID;
@end
Run Code Online (Sandbox Code Playgroud)
store.m
@implementation Store
@synthesize Name;
@synthesize ID;
@synthesize Address;
@synthesize Phone;
@synthesize GeoLat;
@synthesize GeoLong;
/** Implentation of the NSCoding protocol. */
-(void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeInt:ID forKey:@"ID"]; …Run Code Online (Sandbox Code Playgroud) 我有一个我希望保存和加载的自定义类.该类包含NSDate,NSString和NSNumber.我在.h文件中实现了NSCoding协议.这是我到目前为止的代码.theDate是一个NSDate.theName是NSString.homeAway是NSNumber.
-(void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:theDate forKey:@"theDate"];
[aCoder encodeObject:theName forKey:@"theName"];
[aCoder encodeObject:homeAway forKey:@"homeAway"];
}
-(id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super init])) {
theDate = [aDecoder decodeObjectForKey:@"theDate"];
theName = [aDecoder decodeObjectForKey:@"theName"];
homeAway = [aDecoder decodeObjectForKey:@"homeAway"];
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码加载我的自定义对象.当我在调试器中使用print-object时,homeAway只显示为实际对象.theDate和theName说0x4e4f150似乎没有指向有效的对象.
[gamesArray setArray:[NSKeyedUnarchiver unarchiveObjectWithFile:path]];
Game *loadedGame = [gamesArray objectAtIndex:gameNumber];
Run Code Online (Sandbox Code Playgroud)
我使用以下代码保存数据:
我用它来调用我的类来创建一个新游戏(我试图保存的自定义类).代码最多为NSDate*aDate = [gameDate date]; 是无关紧要的.
-(void)newGame {
if (homeAway != 0) {
if (dateChanged == 1) {
if ([nameField.text length] > 0) {
[homeButton setImage:[UIImage imageNamed:@"HomeGray.png"] forState:UIControlStateNormal];
[awayButton setImage:[UIImage imageNamed:@"AwayGray.png"] forState:UIControlStateNormal]; …Run Code Online (Sandbox Code Playgroud) xcode memory-management nscoding nskeyedarchiver saving-data
我试图保存对象NSUserDefaults,在这个网站上经历了很多问题,但无法解决问题,我NSObject有NSMutableArray另一个对象.像这里的主要对象是HotelDC,它有一个数组"features"一个FeactureDC对象数组.
这是我的代码:
- (id)initWithCoder:(NSCoder *)decoder {
self = [[HotelDC alloc] init];
if (self != nil) {
self.hotel_id = [decoder decodeIntegerForKey:@"hotel_id"];
self.name = [decoder decodeObjectForKey:@"name"];
self.features = [decoder decodeObjectForKey:@"features"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeInt:hotel_id forKey:@"hotel_id"];
[encoder encodeObject:name forKey:@"name"];
[encoder encodeObject:features forKey:@"features"];//its a mutable array
}
Run Code Online (Sandbox Code Playgroud)
我该如何保存它并检索?我收到错误了
Attempt to insert non-property value '<HotelDC: 0xa600fe0>' of class 'HotelDC'.
Note that dictionaries and arrays in property lists must also …Run Code Online (Sandbox Code Playgroud) 我花了整个下午的时间撞到墙上,试图弄清楚为什么这门课的解码失败了.该类有一个属性,它是Foo对象的NSArray.Foo符合NSSecureCoding,我已成功编码并解码了该类.我在initWithCoder中收到错误:表示无法解码类Foo.通过一些实验,我发现我需要将[Foo类]添加到initWithCoder:为了使它工作.也许这会帮助那些遇到同样问题的人.我的问题是,为什么这是必要的?我没有发现在Apple的文档中这是必要的.
#import "Foo.h"
@interface MyClass : NSObject <NSSecureCoding>
@property (nonatomic) NSArray *bunchOfFoos;
@end
@implementation MyClass
static NSString *kKeyFoo = @"kKeyFoo";
+ (BOOL) supportsSecureCoding
{
return YES;
}
- (void) encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.bunchOfFoos forKey:kKeyFoo];
}
- (id) initWithCoder:(NSCoder *)decoder
{
if (self = [super init])
{
[Foo class]; // Without this, decoding fails
_bunchOfFoos = [decoder decodeObjectOfClass:[NSArray class] forKey:kKeyFoo];
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud) 由于我们已经重命名了(Bestemming - > Place)类并将其从Objective-c重写为Swift,因此一些用户会遇到崩溃.我们正在尝试使用NSCoding原则从NSUserDefaults加载对象.
碰撞:
Thread : Crashed: com.apple.main-thread
0 Flitsmeister 0x10018b720 specialized Place.init(coder : NSCoder) -> Place? (Place.swift)
1 Flitsmeister 0x10018a6f4 @objc Place.init(coder : NSCoder) -> Place? (Place.swift)
2 Foundation 0x1839ab92c _decodeObjectBinary + 2276
3 Foundation 0x1839aaf90 _decodeObject + 304
4 Foundation 0x1839aa124 +[NSKeyedUnarchiver unarchiveObjectWithData:] + 92
5 Flitsmeister 0x100103fa0 +[SharedUserDefaultsManager WorkPlace] (SharedUserDefaultsManager.m:72)
6 Flitsmeister 0x100090830 -[InvoerBestemmingTableViewController viewWillAppear:] (InvoerBestemmingTableViewController.m:106)
7 UIKit 0x187d8074c -[UIViewController _setViewAppearState:isAnimating:] + 628
8 UIKit 0x187d804c0 -[UIViewController __viewWillAppear:] + 156
9 UIKit 0x187e27130 -[UINavigationController _startTransition:fromViewController:toViewController:] + …Run Code Online (Sandbox Code Playgroud) 我从XCode获取这些崩溃报告(我在底部粘贴了一个),我无法重现错误或弄清楚问题是什么.
从回溯中我认为它可能与这些decodeObject东西有关......我确实遇到了新的decodeBool和decodeDouble函数的问题,所以为了确保我做了一个NSCoder扩展:
func safeDecodeDouble(key: String) -> Double? {
if self.containsValue(forKey: key) {
if let value = self.decodeObject(forKey: key) as? Double {
return value
}
return self.decodeDouble(forKey: key)
}
return nil
}
func safeDecodeBool(key: String) -> Bool? {
if self.containsValue(forKey: key) {
if let value = self.decodeObject(forKey: key) as? Bool {
return value
}
return self.decodeBool(forKey: key)
}
return nil
}
func safeDecodeDate(key: String) -> Date? {
if self.containsValue(forKey: key) {
if let value = …Run Code Online (Sandbox Code Playgroud) 我有以下结构......
struct Photo: Codable {
let hasShadow: Bool
let image: UIImage?
enum CodingKeys: String, CodingKey {
case `self`, hasShadow, image
}
init(hasShadow: Bool, image: UIImage?) {
self.hasShadow = hasShadow
self.image = image
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
hasShadow = try container.decode(Bool.self, forKey: .hasShadow)
// This fails
image = try container.decode(UIImage?.self, forKey: .image)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(hasShadow, forKey: .hasShadow)
// This also fails
try …Run Code Online (Sandbox Code Playgroud) 我有一个界面:
#import <Foundation/Foundation.h>
@interface Picture : NSObject;
@property (readonly) NSString *filepath;
- (UIImage *)image;
@end
Run Code Online (Sandbox Code Playgroud)
和实施:
#import "Picture.h"
#define kFilepath @"filepath"
@interface Picture () <NSCoding> {
NSString *filepath;
}
@end
@implementation Picture
@synthesize filepath;
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:filepath forKey:kFilepath];
}
- (UIImage *)image {
return [UIImage imageWithContentsOfFile:filepath];
}
@end
Run Code Online (Sandbox Code Playgroud)
我收到错误:ARC问题 - 'NSObject'没有可见的@interface声明选择器'initWithCoder:'
使用ARC时,NSCoding有什么不同吗?谢谢
我有以下实现的类,NSCoding我已经创建了它的几个实例并将它们保存到文件中.
@interface BiscuitTin ()
@property NSString *biscuitType;
@property int numBiscuits;
@end
@implementation BiscuitTin
- (id)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
self.biscuitType = [coder decodeObjectForKey:@"biscuitType"];
self.numBiscuits = [coder decodeIntForKey:@"numBiscuits"];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *coder) {
[coder encodeObject:self.biscuitType forKey:@"biscuitType"];
[coder encodeInt:self.numBiscuits forKey:@"numBiscuits"];
}
@end
Run Code Online (Sandbox Code Playgroud)
我现在决定将其表示numBiscuits为float(因为可能有部分吃过的饼干).更新属性类型并且encodeWithCoder工作正常但是当我尝试从文件加载现有实例时,应用程序崩溃,因为它正在尝试解码a int和a float.
有一个很好的方法来处理这个?理想情况下,我可以加载现有int值并将其转换为a float,但我不介意只使用默认值而不是崩溃.
我已经考虑decode在try-catch中包装适用的行,但在我的实际用例中,大约有50个左右的属性正在被编码/解码,并且不必为每个有变化的每个属性进行显式处理类型.
我创建了一个可重用的xib文件,其中包含一个表,并将其加载到如下TableView.swift文件中:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Bundle.main.loadNibNamed("TableView", owner: self, options: nil)
}
Run Code Online (Sandbox Code Playgroud)
我只是提到这一点来澄清我对如何加载xib文件并不感到困惑
我可以RootViewController.swift通过添加视图UIViewController并为该视图提供如下自定义类,轻松地在我的文件中加载可重用视图:
然后为这个视图创建一个这样的出口:
所以这是我的问题:
为什么我不能简单地添加这样的视图:
let tableViewView = TableView()
Run Code Online (Sandbox Code Playgroud)
当我这样做时,我得到一个我不完全理解的错误:
nscoding ×10
ios ×5
objective-c ×4
swift ×3
iphone ×2
cocoa-touch ×1
codable ×1
nsarray ×1
nscoder ×1
saving-data ×1
swift4 ×1
uitableview ×1
xcode ×1
xib ×1