问题描述
我已经阅读了 NSCopying
文档,但我仍然不确定如何实现所需的内容。
我的班级供应商
:
@interface供应商:NSObject
{
NSString * vendorID;
NSMutableArray * availableCars;
BOOL atAirport;
}
@property(非原子,复制)NSString * vendorID;
@property(nonatomic,retain)NSMutableArray * availableCars;
@property(nonatomic,assign)BOOL atAirport;
- (id)initFromVehVendorAvailsDictionary:(NSDictionary *)vehVendorAvails;
@end
供应商
class有一个名为 Car
的对象数组。
我的汽车
对象:
@interface Car:NSObject
{
BOOL isAvailable;
NSString * transmissionType;
NSMutableArray * vehicleCharges;
NSMutableArray *费用;
}
@property(非原子,分配)BOOL isAvailable;
@property(非原子,复制)NSString * transmissionType;
@property(nonatomic,retain)NSMutableArray * vehicleCharges;
@property(非原子,保留)NSMutableArray *费用;
- (id)initFromVehicleDictionary:(NSDictionary *)vehicleDictionary;
@end
所以,供应商
包含一个 Car
对象的数组。 Car
包含2个其他自定义对象数组。
供应商
和 Car
都是字典中的init 。我将添加其中一种方法,它们可能相关也可能不相关。
- (id)initFromVehVendorAvailsDictionary:(NSDictionary * )vehVendorAvails {
self.vendorCode = [[vehVendorAvails objectForKey:@Vendor]
objectForKey:@@ Code];
self.vendorName = [[vehVendorAvails objectForKey:@Vendor]
objectForKey:@@ CompanyShortName];
self.vendorDivision = [[vehVendorAvails objectForKey:@Vendor]
objectForKey:@@ Division];
self.locationCode = [[[vehVendorAvails objectForKey:@Info]
objectForKey:@LocationDetails]
objectForKey:@@ Code];
self.atAirport = [[[[vehVendorAvails objectForKey:@Info]
objectForKey:@LocationDetails]
objectForKey:@@AtAirport] boolValue];
self.venLocationName = [[[vehVendorAvails objectForKey:@Info]
objectForKey:@LocationDetails]
objectForKey:@@ Name];
self.venAddress = [[[[vehVendorAvails objectForKey:@Info]
objectForKey:@LocationDetails]
objectForKey:@Address]
objectForKey:@ AddressLine];
self.venCountryCode = [[[[vehVendorAvails objectForKey:@Info]
objectForKey:@LocationDetails]
objectForKey:@Address]
objectForKey:@CountryName]
objectForKey:@@ Code];
self.venPhone = [[[[vehVendorAvails objectForKey:@Info]
objectForKey:@LocationDetails]
objectForKey:@Telephone]
objectForKey:@ @ ******中国];
availableCars = [[NSMutableArray alloc] init];
NSMutableArray * cars =(NSMutableArray *)[vehVendorAvails objectForKey:@VehAvails];
for(int i = 0; i< [cars count]; i ++){
Car * car = [[Car alloc] initFromVehicleDictionary:[cars objectAtIndex:i ]];
[availableCars addObject:car];
[汽车发布];
}
self.venLogo = [[[vehVendorAvails objectForKey:@Info]
objectForKey:@TPA_Extensions]
objectForKey:@VendorPictureURL] ;
返回自我;
}
总结一下这个可怕的问题。
我需要复制一个 Vendor
对象的数组。我相信我需要在供应商
上实现 NSCopying
协议,这可能意味着我还需要在<$上实现它c $ c> Car ,因为供应商
包含 Car
的数组。这意味着我还需要在属于 Car
对象的2个数组中保存的类上实现它。
供应商上实施
NSCopying
协议,我真的很感激,我可以'在任何地方找到任何教程。实施,你的对象必须响应 -copyWithZone:
选择器。以下是您声明符合它的方式:
@interface MyObject:NSObject< NSCopying> {
然后,在你对象的实现中(你的 .m
文件):
- (id)copyWithZone:(NSZone *)zone
{
//在这里复制代码
}
您的代码应该怎么做?首先,创建一个对象的新实例 - 你可以调用 [[[self class] alloc] init]
来获取当前类的初始化obejct,这对于子类。然后,对于支持复制的 NSObject
的子类的任何实例变量,可以调用 [thatObject copyWithZone:zone]
为新对象。对于原始类型( int
, char
, BOOL
和朋友)只是将变量设置为相等。因此,对于您的obejct供应商,它看起来像这样:
- (id)copyWithZone:(NSZone *)zone
{
id copy = [[[self class] alloc] init];
if(copy){
//复制NSObject子类
[copy setVendorID:[[self.vendorID copyWithZone:zone] autorelease]];
[copy setAvailableCars:[[self.availableCars copyWithZone:zone] autorelease]];
//设置原语
[copy setAtAirport:self.atAirport];
}
返回副本;
}
I've read the NSCopying
docs but I am still very unsure about how to implement what is required.
My class 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
The Vendor
class has an array of objects called Car
.
My Car
object:
@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;
- (id) initFromVehicleDictionary:(NSDictionary *)vehicleDictionary;
@end
So, Vendor
holds an array of Car
objects. Car
holds 2 arrays of other custom objects.
Both Vendor
and Car
are init from a dictionary. I'll add one of these methods, they may or may not be relevant.
-(id)initFromVehVendorAvailsDictionary:(NSDictionary *)vehVendorAvails {
self.vendorCode = [[vehVendorAvails objectForKey:@"Vendor"]
objectForKey:@"@Code"];
self.vendorName = [[vehVendorAvails objectForKey:@"Vendor"]
objectForKey:@"@CompanyShortName"];
self.vendorDivision = [[vehVendorAvails objectForKey:@"Vendor"]
objectForKey:@"@Division"];
self.locationCode = [[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"LocationDetails"]
objectForKey:@"@Code"];
self.atAirport = [[[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"LocationDetails"]
objectForKey:@"@AtAirport"] boolValue];
self.venLocationName = [[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"LocationDetails"]
objectForKey:@"@Name"];
self.venAddress = [[[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"LocationDetails"]
objectForKey:@"Address"]
objectForKey:@"AddressLine"];
self.venCountryCode = [[[[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"LocationDetails"]
objectForKey:@"Address"]
objectForKey:@"CountryName"]
objectForKey:@"@Code"];
self.venPhone = [[[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"LocationDetails"]
objectForKey:@"Telephone"]
objectForKey:@"@PhoneNumber"];
availableCars = [[NSMutableArray alloc] init];
NSMutableArray *cars = (NSMutableArray *)[vehVendorAvails objectForKey:@"VehAvails"];
for (int i = 0; i < [cars count]; i++) {
Car *car = [[Car alloc] initFromVehicleDictionary:[cars objectAtIndex:i]];
[availableCars addObject:car];
[car release];
}
self.venLogo = [[[vehVendorAvails objectForKey:@"Info"]
objectForKey:@"TPA_Extensions"]
objectForKey:@"VendorPictureURL"];
return self;
}
So to summarize the scary problem.
I need to copy an array of Vendor
objects. I believe I need to implement the NSCopying
protocol on Vendor
, which may mean I need to implement it also on Car
since Vendor
holds an array of Car
s. That means I also need to implement it on the classes that are held in the 2 arrays belonging to the Car
object.
I'd really appreciate it if I could get some guidance on implementing NSCopying
protocol on Vendor
, I can't find any tutorials on this anywhere.
To implement NSCopying, your object must respond to the -copyWithZone:
selector. Here’s how you declare that you conform to it:
@interface MyObject : NSObject <NSCopying> {
Then, in your object’s implementation (your .m
file):
- (id)copyWithZone:(NSZone *)zone
{
// Copying code here.
}
What should your code do? First, create a new instance of the object—you can call [[[self class] alloc] init]
to get an initialized obejct of the current class, which works well for subclassing. Then, for any instance variables that are a subclass of NSObject
that supports copying, you can call [thatObject copyWithZone:zone]
for the new object. For primitive types (int
, char
, BOOL
and friends) just set the variables to be equal. So, for your obejct Vendor, it’d look like this:
- (id)copyWithZone:(NSZone *)zone
{
id copy = [[[self class] alloc] init];
if (copy) {
// Copy NSObject subclasses
[copy setVendorID:[[self.vendorID copyWithZone:zone] autorelease]];
[copy setAvailableCars:[[self.availableCars copyWithZone:zone] autorelease]];
// Set primitives
[copy setAtAirport:self.atAirport];
}
return copy;
}
这篇关于实施NSCopying的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!