如何将具有多个参数的对象的新实例输入到 NSMutablearray
How to input new instance of Object with multiple arguments into NSMutablearray
我是 Obj-C 的新手,最近学习了更多 Java 和 C++。
我有两个对象 Friend 和 Foe 继承了角色对象。朋友和敌人的属性略有不同。我希望所有的朋友和敌人都在同一个 NSMutablearray 中。无法弄清楚如何将这些放入数组中。我收到一条错误消息,说参数太多,预计 1 个有 4 个。对于 Foe,它是相同的,但预计 1 个有 5 个。
角色对象
#import <foundation/foundation.h>
@interface Character : NSObject
@property NSString *name;
@property NSInteger strength;
@property NSInteger iff;
- (void) printDetails;
@end
#import <Foundation/Foundation.h>
#import "game_character.h"
@implementation Character
- (void) printDetails
{
NSLog (@"%@ has strength %ld\n", self.name, self.strength);
}
@end
Friend 对象(Foe 对象类似于没有智力和法术但具有备用 NSInteger 属性。
@interface Friend : Character
@property NSInteger intelligence;
@property NSString *spell;
- (void)printDetails;
@end
#import <Foundation/Foundation.h>
#import "game_character.h"
#import "friend.h"
@implementation Friend
-(void)printDetails
{
NSLog (@"%@ has strength %ld\n", self.name, self.strength);
NSLog (@" ,Intelligence %ld, Spell %@\n", self.intelligence, self.spell);
}
@end
朋友输入法(我会有类似的方法输入敌人)
void input_friend()
{
@autoreleasepool
{
char str[30] = {0};
NSInteger strength;
NSInteger iff=1;
NSInteger intelligence;
NSLog(@"Enter character name\n");
scanf("%s", str);
NSString *name = [NSString stringWithUTF8String:str];
NSLog(@"Enter character strength\n");
scanf("%ld", &strength);
NSLog(@"Enter character intelligence");
scanf("%ld", &intelligence);
NSLog(@"Enter character spell\n");
scanf("%s", str);
NSString *spell = [NSString stringWithUTF8String:str];
当我尝试将对象添加到数组时出现错误。
[characters addObject:name, strength, iff, intelligence, spell];
}
}
到目前为止的主要内容。我打算添加一个菜单,其中包含将 Friend 或 Foe 添加到数组的选项。
int main(int argc, const char * argv[])
{
@autoreleasepool
{
characters = [[NSMutableArray alloc] init];
void input_friend();
void input_foe();
}
return 0;
}
在这一行中,您将传递多个参数以添加到您的字符列表中。但是,这不起作用,因为您想添加对象,这就是您收到错误的原因:
[characters addObject:name, strength, iff, intelligence, spell];
所以你需要先初始化一个新的Friend或Foe,设置它的属性,然后将它添加到你的数组中。
Friend *newFriend = [[Friend alloc] init];
newFriend.name = name;
newFriend.strength = strength;
// etc.
[characters addObject: newFriend];
我是 Obj-C 的新手,最近学习了更多 Java 和 C++。 我有两个对象 Friend 和 Foe 继承了角色对象。朋友和敌人的属性略有不同。我希望所有的朋友和敌人都在同一个 NSMutablearray 中。无法弄清楚如何将这些放入数组中。我收到一条错误消息,说参数太多,预计 1 个有 4 个。对于 Foe,它是相同的,但预计 1 个有 5 个。
角色对象
#import <foundation/foundation.h>
@interface Character : NSObject
@property NSString *name;
@property NSInteger strength;
@property NSInteger iff;
- (void) printDetails;
@end
#import <Foundation/Foundation.h>
#import "game_character.h"
@implementation Character
- (void) printDetails
{
NSLog (@"%@ has strength %ld\n", self.name, self.strength);
}
@end
Friend 对象(Foe 对象类似于没有智力和法术但具有备用 NSInteger 属性。
@interface Friend : Character
@property NSInteger intelligence;
@property NSString *spell;
- (void)printDetails;
@end
#import <Foundation/Foundation.h>
#import "game_character.h"
#import "friend.h"
@implementation Friend
-(void)printDetails
{
NSLog (@"%@ has strength %ld\n", self.name, self.strength);
NSLog (@" ,Intelligence %ld, Spell %@\n", self.intelligence, self.spell);
}
@end
朋友输入法(我会有类似的方法输入敌人)
void input_friend()
{
@autoreleasepool
{
char str[30] = {0};
NSInteger strength;
NSInteger iff=1;
NSInteger intelligence;
NSLog(@"Enter character name\n");
scanf("%s", str);
NSString *name = [NSString stringWithUTF8String:str];
NSLog(@"Enter character strength\n");
scanf("%ld", &strength);
NSLog(@"Enter character intelligence");
scanf("%ld", &intelligence);
NSLog(@"Enter character spell\n");
scanf("%s", str);
NSString *spell = [NSString stringWithUTF8String:str];
当我尝试将对象添加到数组时出现错误。
[characters addObject:name, strength, iff, intelligence, spell];
}
}
到目前为止的主要内容。我打算添加一个菜单,其中包含将 Friend 或 Foe 添加到数组的选项。
int main(int argc, const char * argv[])
{
@autoreleasepool
{
characters = [[NSMutableArray alloc] init];
void input_friend();
void input_foe();
}
return 0;
}
在这一行中,您将传递多个参数以添加到您的字符列表中。但是,这不起作用,因为您想添加对象,这就是您收到错误的原因:
[characters addObject:name, strength, iff, intelligence, spell];
所以你需要先初始化一个新的Friend或Foe,设置它的属性,然后将它添加到你的数组中。
Friend *newFriend = [[Friend alloc] init];
newFriend.name = name;
newFriend.strength = strength;
// etc.
[characters addObject: newFriend];