如何使用scanf
函数而不是通过目标c在代码中包含信息?
#import <Foundation/Foundation.h>
@interface Person: NSObject
{
NSString *personname;
int personage;
int personweight;
int personheight;
}
-(void) setPersonName:(NSString *)name;
-(void) setPersonAge:(int)age;
-(void) setPersonWeight:(int)weight;
-(void) setPersonHeight:(int)Height;
-(void) SetprintPerson;
@end
@implementation Person
-(void) setPersonName:(NSString *)name
{
personname = name;
}
-(void) setPersonAge:(int)age
{
personage = age;
}
-(void) setPersonWeight:(int)weight
{
personweight = weight;
}
-(void) setPersonHeight:(int)Height
{
personheight = Height;
}
-(void) SetprintPerson
{
NSLog(@"\n person name: %@ \n person age: %i \n person weight: %i KG \n person Height: %i CM",personname,personage,personweight,personheight);
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool
{
// I wont to use the scanf here .
Person *user = [[Person alloc]init];
[user setPersonName:@"ahmed"];
[user setPersonAge:30];
[user setPersonWeight:176];
[user setPersonHeight:90];
[user SetprintPerson];
}
return 0;
}
我正在学习,并且按照要求在工作中遇到了困难。
答案 0 :(得分:0)
scanf只是让你读取一个字符数组 所以你需要这样的东西:
char str[100]; //a C char Array with 100 Chars
scanf("%99s", str); //leave the last one empty becaue of '\0'
[user setPersonName:[NSString stringWithUTF8String:str]]; //initialize a new NSString
整数很简单,你可以像这样阅读:
int age;
scanf("%d", &age);
[user setPersonAge: age];