Iphone 如何使(NSString*)文本与NSTimer一起工作?
我不能在NSTimer上使用pokeme:@“1”或pokeme:1或pokeme:“sdfsdfs”。 我有错误出现。你怎么解决这个问题Iphone 如何使(NSString*)文本与NSTimer一起工作?,iphone,string,sdk,nstimer,Iphone,String,Sdk,Nstimer,我不能在NSTimer上使用pokeme:@“1”或pokeme:1或pokeme:“sdfsdfs”。 我有错误出现。你怎么解决这个问题 - (void)Anything { [NSTimer scheduledTimerWithTimeInterval:06.00f target:self selector:@selector(Pokeme:@"1") userInfo:nil repeats:NO]; } - (void)Pokeme:(NSString *)text { }
- (void)Anything {
[NSTimer scheduledTimerWithTimeInterval:06.00f target:self selector:@selector(Pokeme:@"1") userInfo:nil repeats:NO];
}
- (void)Pokeme:(NSString *)text {
}
您不能这样做-选择器只是一个方法的名称,该方法的参数将是计时器。您必须创建一个包含所需行为的新方法并将其作为选择器传递,或者使用类似的方法。您没有正确调用选择器,也没有在-Pokeme:上使用正确的参数
这不是有效的选择器。选择器只是方法签名方法的名称,您不能传递它参数,因此您应该具有
[NSTimer scheduledTimerWithTimeInterval:6.00f target:self selector:@selector(pokeMe:) userInfo:nil repeats:NO];
根据文件规定,接收方式的签名应在表格之外
- (void)timerFireMethod:(NSTimer *)theTimer
因此,您应该将您的方法定义为
- (void)pokeMe:(NSTimer *)timer;
如果要沿userInfo
参数传递额外信息,则接受一种类型的id
,并且可以从timer
对象中检索
一个有效的例子是
[NSTimer scheduledTimerWithTimeInterval:06.00f target:self selector:@selector(pokeMe:) userInfo:@"I was passed" repeats:NO];
然后
看
这不是传递变量的正确方法,实际上会抛出编译器。使用userInfo
传递对象,然后您可以将该对象转换为NSString
或任何您需要的内容
/* INCORRECT */
[NSTimer scheduledTimerWithTimeInterval:06.00f target:self
selector:@selector(Pokeme:@"1") userInfo:nil repeats:NO];
基本上,userInfo
可以是一个对象:
NSString
字典
有点吹毛求疵,但选择器是方法的名称-方法的签名由NSMethodSignature表示。@Chuck我不认为提高某人的知识和纠正错误是“吹毛求疵”。谢谢
/* INCORRECT */
[NSTimer scheduledTimerWithTimeInterval:06.00f target:self
selector:@selector(Pokeme:@"1") userInfo:nil repeats:NO];
/* Pass @"1" as the userInfo object. */
[NSTimer scheduledTimerWithTimeInterval:6 target:self
selector:@selector(Pokeme:) userInfo:@"1" repeats:NO];
/* Convert the object to NSString. */
-(void)Pokeme:(NSTimer*)timer {
NSString *passedString = (NSString*)timer.userInfo;
}
/* Create an NSDictionary with 2 Key/Value objects. */
NSDictionary *passTheseKeys = [NSDictionary dictionaryWithObjectsAndKeys:
@"Some String Value 1", @"StringKey1",
@"Some String Value 2", @"StringKey2", nil];
/* Pass the NSDictionary we created above. */
[NSTimer scheduledTimerWithTimeInterval:6 target:self
selector:@selector(Pokeme:) userInfo:passTheseKeys repeats:NO];
/* Convert the timer object to NSDictionary and handle it in the usual way. */
- (void)Pokeme:(NSTimer*)timer {
NSDictionary *passed = (NSDictionary *)[timer userInfo];
NSString * objectString1 = [passed objectForKey:@"StringKey1"];
NSString * objectString2 = [passed objectForKey:@"StringKey2"];
}