iOS シンタックスに関する、NStimer の scheduledTimerWithTimeInterval メソッドのパラメータの問題

scheduledTimerWithTimeIntervalメソッドでNSTimerを作成する際、引数が必要な場合はuserInfoパラメータを使用して追加データを渡すことができます。

以下にサンプルコードを示します。

- (void)startTimerWithInterval:(NSTimeInterval)interval {
NSDictionary *userInfo = @{@"param1": @"value1", @"param2": @"value2"};
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(timerFired:)
userInfo:userInfo
repeats:YES];
}
- (void)timerFired:(NSTimer *)timer {
NSDictionary *userInfo = timer.userInfo;
NSString *param1 = userInfo[@"param1"];
NSString *param2 = userInfo[@"param2"];
// 使用传递的参数进行相关操作
NSLog(@"param1: %@, param2: %@", param1, param2);
}

startTimerWithIntervalメソッドでは、userInfoパラメータを使用して必要なパラメータをNSDictionaryオブジェクトに保存します。次に、timerFired:メソッドでtimer.userInfoを使用して渡されたパラメータを取得し、関連する操作を実行します。

bannerAds