You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ARC下禁止long转id及项目迁移ARC代码求助 - iOS开发

iOS ARC Migration & Type Conversion Issues: Practical Solutions

Hey there! I’ve been right where you are—migrating a legacy iOS project to ARC and hitting those annoying type conversion and memory management snags. Let’s break down your two problems with straightforward fixes.

1. ARC Error: Cannot Convert 'long' to 'id'

First off, ARC only manages Objective-C objects (the id type refers to any Objective-C object). A long is a primitive C data type, not an object, so ARC throws an error when you try to cast it directly to id—it doesn’t know how to handle memory for primitives (since they don’t use reference counting).

Fix: Wrap the primitive in an NSNumber

Objective-C provides NSNumber to wrap primitive types into objects that ARC can manage. Here’s how to implement it:

Converting long to id (object):

long myLongValue = 12345;
id wrappedValue = @(myLongValue); // Shorthand for [NSNumber numberWithLong:myLongValue]

Converting back from id to long:

If you need to retrieve the primitive value later:

long originalValue = [wrappedValue longValue];

Example of fixing problematic code:
If you had this error-prone line:

// Error: Cannot cast 'long' to 'id'
myDictionary[@"someKey"] = (id)myLongValue;

Replace it with:

myDictionary[@"someKey"] = @(myLongValue);

2. ARC Migration for File Upload/Download Code (Focus on Final Lines)

When moving non-ARC code to ARC, the biggest fix is removing manual memory management calls that ARC now handles automatically. The "last line" of your upload/download code is almost certainly a release, autorelease, or retain call—these are strictly forbidden in ARC.

Common Scenarios & Fixes

Scenario 1: Non-ARC upload code with manual release

Suppose your old upload code ends with:

NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] initWithURL:uploadURL];
// ... set up request body, headers, etc.
[uploadRequest release]; // Manual release—REMOVE THIS IN ARC!

ARC Fix: Delete the [uploadRequest release]; line. ARC will automatically release the request when it goes out of scope.

Scenario 2: Non-ARC download code returning an autoreleased object

If your old download code returns an autoreleased object:

- (NSData *)downloadFileFromURL:(NSURL *)url {
    NSData *fileData = [[NSData alloc] initWithContentsOfURL:url];
    return [fileData autorelease]; // Autorelease—REMOVE THIS IN ARC!
}

ARC Fix: Delete the autorelease call. In ARC, you can return the object directly—ARC handles the memory automatically:

- (NSData *)downloadFileFromURL:(NSURL *)url {
    NSData *fileData = [[NSData alloc] initWithContentsOfURL:url];
    return fileData;
}

Or even better, use the convenience constructor (ARC manages these objects seamlessly):

- (NSData *)downloadFileFromURL:(NSURL *)url {
    return [NSData dataWithContentsOfURL:url];
}

Bonus: Modernize with NSURLSession (Recommended for ARC)

If your code uses the old NSURLConnection API, switch to NSURLSession—it’s built for ARC, uses blocks for callbacks, and avoids delegate memory headaches. Here’s a quick async download example:

- (void)downloadFileAsyncWithURL:(NSURL *)url completion:(void(^)(NSData *data, NSError *error))completion {
    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (completion) {
            completion(data, error);
        }
    }];
    [task resume];
}

No manual memory management needed here—ARC takes care of the task and session objects.

General ARC Migration Tips for These Files:

  • Remove all retain, release, autorelease, and empty dealloc methods that only contain release calls.
  • Update property declarations: change @property (retain) to @property (strong), keep assign for primitives, and keep copy for strings/immutable objects.
  • If using custom NSOperation subclasses, ensure you’re not manually managing their memory—ARC will handle it.

内容的提问来源于stack exchange,提问作者user275472

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:21:40