gpt4 book ai didi

iphone - 实现 NSCopying

转载 作者:IT老高 更新时间:2023-10-28 11:23:33 25 4
gpt4 key购买 nike

我已阅读 NSCopying 文档,但我仍然不确定如何实现所需的内容。

我的类(class)供应商:

@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

Vendor 类有一个名为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;

- (id) initFromVehicleDictionary:(NSDictionary *)vehicleDictionary;

@end

因此,Vendor 拥有一个 Car 对象数组。 Car 包含 2 个其他自定义对象的数组。

VendorCar 都是从字典中初始化的。我将添加其中一种方法,它们可能相关也可能不相关。

-(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;
}

所以总结一下这个可怕的问题。

我需要复制一组 Vendor 对象。我相信我需要在 Vendor 上实现 NSCopying 协议(protocol),这可能意味着我也需要在 Car 上实现它,因为 Vendor 包含 Car 的数组。这意味着我还需要在属于 Car 对象的 2 个数组中保存的类上实现它。

如果我能得到一些关于在 Vendor 上实现 NSCopying 协议(protocol)的指导,我将不胜感激,我在任何地方都找不到这方面的任何教程。

最佳答案

实现NSCopying ,您的对象必须响应 -copyWithZone: 选择器。以下是您声明遵守它的方式:

@interface MyObject : NSObject <NSCopying> {

然后,在你的对象的实现中(你的 .m 文件):

- (id)copyWithZone:(NSZone *)zone
{
// Copying code here.
}

您的代码应该做什么?首先,创建对象的一个​​新实例——您可以调用 [[[self class] alloc] init] 来获取当前类的初始化对象,这对于子类化很有效。然后,对于任何支持复制的 NSObject 子类的实例变量,您可以为新对象调用 [thatObject copyWithZone:zone]。对于原始类型(intcharBOOL 和 friend ),只需将变量设置为相等即可。因此,对于您的对象供应商,它看起来像这样:

- (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;
}

关于iphone - 实现 NSCopying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4089238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com