gpt4 book ai didi

iphone - 新的更好的初始化类和对象的方法

转载 作者:行者123 更新时间:2023-12-03 20:55:15 25 4
gpt4 key购买 nike

这是我想用来初始化我的选择器的代码。有更好的写法吗?

界面

@interface roomsClass : NSObject {
NSInteger roomID;
NSString *name;
NSString *desc;
}

@property (nonatomic, readwrite) NSInteger roomID;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *desc;

@end

@interface typesClass : NSObject {
NSInteger typeID;
NSString *name;
}

@property (nonatomic, readwrite) NSInteger typeID;
@property (nonatomic, copy) NSString *name;

@end

实现

NSMutableArray *tempArrayA = [[NSMutableArray alloc] init];
self.roomsArray = tempArrayA;
[tempArrayA release];

roomsClass *tempObjA1 = [[roomsClass alloc] init];
tempObjA1.roomID = 202;
tempObjA1.name = @"MH202";
tempObjA1.desc = @"Lab";
[roomsArray addObject:tempObjA1];
[tempObjA1 release];

roomsClass *tempObjA2 = [[roomsClass alloc] init];
tempObjA2.roomID = 207;
tempObjA2.name = @"MH207";
tempObjA2.desc = @"Office";
[roomsArray addObject:tempObjA2];
[tempObjA2 release];

roomsClass *tempObjA3 = [[roomsClass alloc] init];
tempObjA3.roomID = 208;
tempObjA3.name = @"MH208";
tempObjA3.desc = @"Office";
[roomsArray addObject:tempObjA3];
[tempObjA3 release];

roomsClass *tempObjA4 = [[roomsClass alloc] init];
tempObjA4.roomID = 209;
tempObjA4.name = @"MH209";
tempObjA4.desc = @"Lab";
[roomsArray addObject:tempObjA4];
[tempObjA4 release];

roomsClass *tempObjA5 = [[roomsClass alloc] init];
tempObjA5.roomID = 210;
tempObjA5.name = @"MH210";
tempObjA5.desc = @"Lab";
[roomsArray addObject:tempObjA5];
[tempObjA5 release];

roomsClass *tempObjA6 = [[roomsClass alloc] init];
tempObjA6.roomID = 211;
tempObjA6.name = @"MH211";
tempObjA6.desc = @"Office";
[roomsArray addObject:tempObjA6];
[tempObjA6 release];

NSMutableArray *tempArrayB = [[NSMutableArray alloc] init];
self.typesArray = tempArrayB;
[tempArrayB release];

typesClass *tempObjB1 = [[typesClass alloc] init];
tempObjB1.typeID = 1;
tempObjB1.name = @"Desktop";
[typesArray addObject:tempObjB1];
[tempObjB1 release];

typesClass *tempObjB2 = [[typesClass alloc] init];
tempObjB2.typeID = 2;
tempObjB2.name = @"Laptop";
[typesArray addObject:tempObjB2];
[tempObjB2 release];

typesClass *tempObjB3 = [[typesClass alloc] init];
tempObjB3.typeID = 3;
tempObjB3.name = @"Monitor";
[typesArray addObject:tempObjB3];
[tempObjB3 release];

typesClass *tempObjB4 = [[typesClass alloc] init];
tempObjB4.typeID = 4;
tempObjB4.name = @"Server";
[typesArray addObject:tempObjB4];
[tempObjB4 release];

typesClass *tempObjB5 = [[typesClass alloc] init];
tempObjB5.typeID = 5;
tempObjB5.name = @"Printer";
[typesArray addObject:tempObjB5];
[tempObjB5 release];

typesClass *tempObjB6 = [[typesClass alloc] init];
tempObjB6.typeID = 6;
tempObjB6.name = @"Projector";
[typesArray addObject:tempObjB6];
[tempObjB6 release];

typesClass *tempObjB7 = [[typesClass alloc] init];
tempObjB7.typeID = 7;
tempObjB7.name = @"Other";
[typesArray addObject:tempObjB7];
[tempObjB7 release];


if (roomPicker == nil) {
roomPicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
roomPicker.delegate = self;
roomPicker.dataSource = self;
roomPicker.showsSelectionIndicator = YES;
roomPicker.transform = CGAffineTransformMakeScale(1.0, 1.0);
roomPicker.tag = kRoomPicker;
}

txtRoomID.inputView = roomPicker;


if (typePicker == nil) {
typePicker = [[UIPickerView alloc] initWithFrame:CGRectZero];
typePicker.delegate = self;
typePicker.dataSource = self;
typePicker.showsSelectionIndicator = YES;
typePicker.transform = CGAffineTransformMakeScale(1.0, 1.0);
typePicker.tag = kTypePicker;
}

txtTypeID.inputView = typePicker;

这个想法是让选择器显示一些文本,但是当用户在 UIPickerView 上选择文本时,选择的整数值/id 将放置在 UITextField 中,而不是用户看到的文本。

我在设备上使用 iOS 4.3.1、4.3 SDK 和 Xcode 3.2.6。

最佳答案

减少冗余的一种方法称为便利构造函数。部分实现如下所示:

@interface MONRoomsClass : NSObject {
NSInteger roomID;
NSString *name;
NSString *desc;
}

@property (nonatomic, readwrite) NSInteger roomID;
@property (nonatomic, copy) NSString *name;
/* note: ivar would normally be named 'description' */
@property (nonatomic, copy) NSString *desc;

/* designated initializer */
- (id)initWithRoomID:(NSInteger)inRoomID
name:(NSString *)inName
desc:(NSString *)inDesc;

/* convenience constructor */
+ (MONRoomsClass *)newRoomsClassWithRoomID:(NSInteger)inRoomID
name:(NSString *)inName
desc:(NSString *)inDesc;

/* convenience constructor */
+ (MONRoomsClass *)roomsClassWithRoomID:(NSInteger)inRoomID
name:(NSString *)inName
desc:(NSString *)inDesc;

@end

@implementation MONRoomsClass

- (id)initWithRoomID:(NSInteger)inRoomID
name:(NSString *)inName
desc:(NSString *)inDesc
{
/* error checking omitted */
self = [super init];
if (0 != self) {
roomID = inRoomID;
name = [inName copy];
desc = [inDesc copy];

}
return self;
}

+ (MONRoomsClass *)newRoomsClassWithRoomID:(NSInteger)inRoomID
name:(NSString *)inName
desc:(NSString *)inDesc
{
return [[self alloc] initWithRoomID:inRoomID name:[inName desc:inDesc];
}

+ (MONRoomsClass *)roomsClassWithRoomID:(NSInteger)inRoomID
name:(NSString *)inName
desc:(NSString *)inDesc
{
return [[[self alloc] initWithRoomID:inRoomID
name:inName
desc:inDesc] autorelease];
}

@end

void in_use() {

/* ... */

MONRoomsClass * roomsClass =
[MONRoomsClass newRoomsClassWithRoomID:210
name:@"MH210"
desc:@"Lab"];
[roomsArray addObject:roomsClass];

/* or */
[roomsArray addObject:
[[MONRoomsClass newRoomsClassWithRoomID:210
name:@"MH210"
desc:@"Lab"]
autorelease]];
/* or */
[roomsArray addObject:
[MONRoomsClass roomsClassWithRoomID:210
name:@"MH210"
desc:@"Lab"]];

/* ... */

}

关于iphone - 新的更好的初始化类和对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5697935/

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