I have a variable usuario
of type TuplaUsuario
. When I insert its data, I want to initialize NSMutableArray cups
to an Array
of type TuplaCups
. How can I do it?
我有一个类型TuplaUsuario的变量usuario。当我插入其数据时,我想将NSMutableArray杯初始化为TuplaCups类型的数组。我该怎么做?
Here is my code up to date:
这是我最新的代码:
TuplaUsuario:
//TuplaUsuario.h:
@interface TuplaUsuario : NSObject
{
NSMutableString* mensaje;
NSString* usuario;
NSString* password;
NSMutableArray* cups;
//More variables
}
//Property for each variable
- (id)initWithString:(NSString *)identifier;
//TuplaUsuario.m:
//Synthesize for each variable
- (id)initWithString:(NSString *)identifier {
if ( self = [super init] ) {
mensaje = [[NSMutableString alloc] initWithString:identifier];
usuario = [[NSString alloc] initWithString:identifier];
password = [[NSString alloc] initWithString:identifier];
cups = [[NSMutableArray alloc] init];
}
return self;
}
TuplaCups:
//TuplaCups.h:
@interface TuplaCups : NSObject {
NSString* cups;
NSString* tarifa;
NSString* direccion;
NSString* nombreUsuario;
NSMutableArray* facturas;
}
//Property for each variable
- (id)initWithString:(NSString *)identifier;
//TuplaCups.m:
//Synthesize for each variable
- (id)initWithString:(NSString *)identifier {
if ( self = [super init] ) {
cups = [[NSMutableString alloc] initWithString:identifier];
tarifa = [[NSString alloc] initWithString:identifier];
direccion = [[NSString alloc] initWithString:identifier];
nombreUsuario = [[NSString alloc] initWithString:identifier];
facturas = [[NSMutableArray alloc] init];
}
return self;
}
2 个解决方案
#1
1
Arrays (as in NSArray and NSMutableArray) in Objective-C are not typed, they can hold any object type and you can't easily restrict what's going in. So you are responsible for ensuring that only objects of the type you want go in. Usually, this isn't a problem unless you expose the mutable array so that other objects might put stuff into your array. In this case, it's better to provide accessors:
Objective-C中的数组(如在NSArray和NSMutableArray中)没有输入,它们可以保存任何对象类型,并且您不能轻易限制进入的内容。因此,您有责任确保只有您想要的类型的对象进入。通常,这不是问题,除非您公开可变数组,以便其他对象可能将东西放入您的数组中。在这种情况下,最好提供访问者:
- (void)addFoo:(Foo *)foo {
[myMutableArray addObject:foo];
}
- (void)removeFoo:(Foo *)foo {
[myMutableArray removeObject:foo];
}
// Return an array of Foo instances.
- (NSArray *)foos {
return [[myMutableArray copy] autorelease];
}
#2
1
Objective-C doesn't have a concept of Generics like C# or C++ do. There are no templates after all. You just have to know what type of objects you've put into an array. If you are going to intermix them, you can use isKindOfClass:
to test for the class.
Objective-C没有像C#或C ++这样的泛型概念。毕竟没有模板。您只需要知道放入数组的对象类型。如果要混用它们,可以使用isKindOfClass:来测试类。
#1
1
Arrays (as in NSArray and NSMutableArray) in Objective-C are not typed, they can hold any object type and you can't easily restrict what's going in. So you are responsible for ensuring that only objects of the type you want go in. Usually, this isn't a problem unless you expose the mutable array so that other objects might put stuff into your array. In this case, it's better to provide accessors:
Objective-C中的数组(如在NSArray和NSMutableArray中)没有输入,它们可以保存任何对象类型,并且您不能轻易限制进入的内容。因此,您有责任确保只有您想要的类型的对象进入。通常,这不是问题,除非您公开可变数组,以便其他对象可能将东西放入您的数组中。在这种情况下,最好提供访问者:
- (void)addFoo:(Foo *)foo {
[myMutableArray addObject:foo];
}
- (void)removeFoo:(Foo *)foo {
[myMutableArray removeObject:foo];
}
// Return an array of Foo instances.
- (NSArray *)foos {
return [[myMutableArray copy] autorelease];
}
#2
1
Objective-C doesn't have a concept of Generics like C# or C++ do. There are no templates after all. You just have to know what type of objects you've put into an array. If you are going to intermix them, you can use isKindOfClass:
to test for the class.
Objective-C没有像C#或C ++这样的泛型概念。毕竟没有模板。您只需要知道放入数组的对象类型。如果要混用它们,可以使用isKindOfClass:来测试类。