未使用的变量

时间:2014-11-11 08:43:06

标签: xcode6 push

我在xcode中有相同的警告(未使用的变量'conn')。

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSString *token = [deviceToken description];
    token = [token stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];

    [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"deviceToken"];

    NSData *postData = [[NSString stringWithFormat:@"token=%@", deviceToken] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    //Don't forget to change the API link to your own link
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:
                                          @"http://mydomaine.com/push/savetoken/?device_token=%@&device_type=ios&channels_id=1", token]]];

    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    NSLog(@"My device token is: %@", deviceToken);
}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
    NSLog(@"Failed to get token, error: %@", error);
}

由于

1 个答案:

答案 0 :(得分:0)

编译器是正确的;你创建了一个NSURLConnection的实例,当它超出范围时会立即销毁。

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

我怀疑你想使用实例变量或@property来保持对连接的引用:

@interface AppDelegate ()
{
    NSURLConnection *_conn;
}
@end

并使用:

_conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];