基于用户输入的 Setter 方法
Objective-C 新手,正在尝试完成课程的期末项目。我创建了一种根据用户输入设置日期的方法,但设置器遇到了一些麻烦。用户首先必须选择添加新条目的选项,然后程序应要求用户输入日期并根据该输入设置日期。
#import <Foundation/Foundation.h>
@interface Planner : NSObject {
NSNumber *date;
}
-(void) setDate:(NSNumber *)newDate;
-(NSNumber *) date;
@end
@implementation Planner
-(void) setDate:(NSNumber *) newDate
{
date = [[NSNumber alloc] initWithInt: newDate];
NSLog(@"Enter date");
scanf("%i", newDate);
}
-(NSNumber *) date;
{
return date;
}
@end
int main (int argc, const char * argv[])
{
int userAction;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Planner *newPlanner = [[Planner alloc] init];
NSLog(@"Please enter 1 to add a new entry and 2 to update an existing entry");
scanf("%i", userAction);
if (userAction == 1) {
[newPlanner setDate];
}
else
NSLog(@"will update");
[pool drain];
return 0;
}
New to Objective-C and trying to complete a final project for a class. I have created a method to set a date based on user input and am running into some trouble with the setter. User will first have to select option to add new entry and then program should ask user to enter a date and set date based on that input.
#import <Foundation/Foundation.h>
@interface Planner : NSObject {
NSNumber *date;
}
-(void) setDate:(NSNumber *)newDate;
-(NSNumber *) date;
@end
@implementation Planner
-(void) setDate:(NSNumber *) newDate
{
date = [[NSNumber alloc] initWithInt: newDate];
NSLog(@"Enter date");
scanf("%i", newDate);
}
-(NSNumber *) date;
{
return date;
}
@end
int main (int argc, const char * argv[])
{
int userAction;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Planner *newPlanner = [[Planner alloc] init];
NSLog(@"Please enter 1 to add a new entry and 2 to update an existing entry");
scanf("%i", userAction);
if (userAction == 1) {
[newPlanner setDate];
}
else
NSLog(@"will update");
[pool drain];
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setDate
应该接收NSNumber*
类型的参数。但是您正在调用该方法而不传递任何参数。喜欢 -
setDate
is supposed to receive an argument of typeNSNumber*
. But you are calling the method with out passing any parameter.Like -