我已经阅读了NSCopying文档,但我仍然不确定如何实现所需的内容.
我的班级Vendor:
@interface Vendor : NSObject
{
NSString *vendorID;
NSMutableArray *availableCars;
BOOL atAirport;
}
@property (nonatomic, copy) NSString *vendorID;
@property (nonatomic, retain) NSMutableArray *availableCars;
@property (nonatomic, assign) BOOL atAirport;
- (id)initFromVehVendorAvailsDictionary:(NSDictionary *)vehVendorAvails;
@end
Run Code Online (Sandbox Code Playgroud)
的Vendor类有称为对象的数组Car.
我的Car目标:
@interface Car : NSObject
{
BOOL isAvailable;
NSString *transmissionType;
NSMutableArray *vehicleCharges;
NSMutableArray *fees;
}
@property (nonatomic, assign) BOOL isAvailable;
@property (nonatomic, copy) NSString *transmissionType;
@property (nonatomic, retain) NSMutableArray *vehicleCharges;
@property (nonatomic, retain) NSMutableArray *fees;
- …Run Code Online (Sandbox Code Playgroud) 在一个或一个上使用copy和之间有什么区别?mutableCopyNSArrayNSMutableArray
这是我的理解; 这是对的吗?
// ** NSArray **
NSArray *myArray_imu = [NSArray arrayWithObjects:@"abc", @"def", nil];
// No copy, increments retain count, result is immutable
NSArray *myArray_imuCopy = [myArray_imu copy];
// Copys object, result is mutable
NSArray *myArray_imuMuta = [myArray_imu mutableCopy];
// Both must be released later
Run Code Online (Sandbox Code Playgroud)
// ** NSMutableArray **
NSMutableArray *myArray_mut = [NSMutableArray arrayWithObjects:@"A", @"B", nil];
// Copys object, result is immutable
NSMutableArray *myArray_mutCopy = [myArray_mut copy];
// Copys object, result is mutable
NSMutableArray *myArray_mutMuta …Run Code Online (Sandbox Code Playgroud) 我希望有一个NSDictionary从UIViews 映射到其他东西.
但是,由于UIViews没有实现NSCopying协议,我不能直接将它们用作字典键.
我有一个小类层次结构,我无法实现copyWithZone:.我已经阅读了NSCopying文档,但我找不到正确的答案.
选择两个类:Shape和Square.Square定义为:
@interface Square : Shape
Run Code Online (Sandbox Code Playgroud)
这并不奇怪.每个类都有一个属性,Shape有一个"sides"int,而Square有一个"width"int.该copyWithZone:方法被认为如下:
形状
- (id)copyWithZone:(NSZone *)zone {
Shape *s = [[Shape alloc] init];
s.sides = self.sides;
return s;
}
Run Code Online (Sandbox Code Playgroud)
广场
- (id)copyWithZone:(NSZone *)zone {
Square *s = (Square *)[super copyWithZone:zone];
s.width = self.width;
return s;
}
Run Code Online (Sandbox Code Playgroud)
看一下文档,这似乎是做事的"正确"方式.
它不是.
如果您尝试设置/访问copyWithZone:方法返回的Square的width属性,它将失败,并出现类似于下面的错误:
2010-12-17 11:55:35.441 Hierarchy[22617:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Shape setWidth:]: unrecognized selector sent to instance 0x10010c970'
Run Code Online (Sandbox Code Playgroud)
调用[super …
在我的应用程序中,我NSDictionary的键应该是子类的实例NSManagedObject.
然而,问题是NSManagedObject没有实现NSCopying协议,这意味着没有Core Data对象/实例NSManagedObject可以用作字典键,即使该-[hash]方法适用于它们.
我该怎么办?
在类中实现此NSCopying方法以启用复制时,区域参数的用途是什么?如果我设置一个新对象,我不需要用allocWithZone分配它,因为一个分配就够了......我很困惑......
这可能是一个简单的问题,但为什么在我的课堂上实施NSCopying协议,我得到zone == nil
- (id)copyWithZone:(NSZone *)zone
{
if (zone == nil)
NSLog(@"why this is allways nil");
(...)
}
Run Code Online (Sandbox Code Playgroud)
对于带有对象的复制数组,使用此方法调用此方法.
[[NSArray alloc] initWithArray:myArray copyItems:YES]];
Run Code Online (Sandbox Code Playgroud) 我知道如果您的对象将被用作NSDictionary中的键,则需要它.还有其他这样的时间需要NSCopying吗?
如果我认为我不需要我的模型对象符合NSCopying,我可能错了吗?
我可能会遗漏一些明显的东西,但我正在我的一个对象上实现NSCopying.该对象具有未通过getter公开的私有实例变量,因为它们不应在对象外部使用.
在我的实现中copyWithZone:,我需要alloc/init新实例,还要设置其状态以匹配当前实例.我显然可以从内部访问当前的私有状态copyWithZone:,但我无法将其设置为新对象,因为该状态没有访问器.
有没有一种标准的方法来保持数据隐私的完整性?
谢谢.
nscopying ×10
objective-c ×6
cocoa ×4
iphone ×3
nsdictionary ×3
ios ×2
nsarray ×2
clang ×1
cocoa-touch ×1
copywithzone ×1
core-data ×1
foundation ×1
hierarchy ×1
instancetype ×1
uiview ×1
xcode ×1