Correct bridging for ARC?

后端 未结 4 673
感情败类
感情败类 2020-12-04 14:25

I have a category class for NSString.

@implementation NSString (URLEncode)

- (NSString *)URLEncodedString
{
    __autoreleasing NSString *encodedString;

          


        
4条回答
  •  时光说笑
    2020-12-04 14:56

    As mentioned in the comments, I think it's fine to talk about ARC and the contents of Automatic Reference Counting here.

    __autoreleasing is not meant to be used like that. It's used for passing indirect object references (NSError**, etc). See 4.3.4 Passing to an out parameter by writeback.

    According to 3.2.4 Bridged casts, the __bridge_transfer is correct as the CFURLCreateStringByAddingPercentEscapes function returns a retained object (it has "create" in its name). You want ARC to take ownership of the returned object and insert a release (or autorelease in this case) to balance this out.

    The __bridge cast for originalstring is correct too, you don't want ARC to do anything special about it.

提交回复
热议问题