标签: automatic-ref-counting

Xcode 4.2功能 - ARC与添加标志相同

在xCode 4.2中包含一个新的"迁移到Objective-C ARC"工具Edit->Refactor.这与添加标志相同-fno-objc-arc

iphone objective-c xcode4.2 automatic-ref-counting

1
推荐指数
1
解决办法
165
查看次数

iOS 4.3是否支持ARC

我尝试谷歌搜索,但我无法找到苹果文档,他们说ARC - automatic reference counting支持iOS 4.3.谁能告诉我这是否属实?如果是这样,你可以给我发一个指向苹果文档的链接.

iphone cocoa-touch objective-c automatic-ref-counting

1
推荐指数
1
解决办法
1718
查看次数

iOS 5需要ARC桥接转换

我遵循一个教程,我不知道如何转换此代码,以使其免费运行,没有启用ARC的错误.

- (void)setHourHandImage:(CGImageRef)image
{
if (image == NULL) {
    hourHand.backgroundColor = [UIColor blackColor].CGColor;
    hourHand.cornerRadius = 3;
}else{
    hourHand.backgroundColor = [UIColor clearColor].CGColor;
    hourHand.cornerRadius = 0.0;

}
hourHand.contents = (id)image;
Run Code Online (Sandbox Code Playgroud)

给我一个错误的唯一部分是(id)图像;

w = CGImageGetWidth((CGImageRef)hourHand.contents);
Run Code Online (Sandbox Code Playgroud)

(CGImageRef)minHand.contents); 给了我一个错误

谢谢

xcode image ios automatic-ref-counting

1
推荐指数
1
解决办法
8305
查看次数

使用ARC处理NSError - 泄漏

- (BOOL)parserJSONString:(NSString *)jsonString error:(NSError **)anError {
   //some data getting
   //error handle

    NSString *description = @"phone number couldn't be using";
    NSString *recoverySuggestion = @"Please provide an other phone number.";
    NSInteger errorCode = -1;
    NSArray *keys = [NSArray arrayWithObjects: NSLocalizedDescriptionKey, NSLocalizedRecoverySuggestionErrorKey, nil];
    NSArray *values = [NSArray arrayWithObjects:description, recoverySuggestion, nil];
    NSDictionary *userDict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    *anError = [[NSError alloc] initWithDomain:@"my domain" code:errorCode userInfo:userDict];
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

*anError = [[NSError alloc] initWithDomain:@"my domain" code:errorCode userInfo:userDict];编译器给出下一个泄漏警告"潜在的空取消引用.根据'创建和返回NSError对象'中的编码标准,参数''可能为空"
如何解决这个问题?

iphone objective-c ipad ios automatic-ref-counting

1
推荐指数
2
解决办法
2580
查看次数

ARC的运行时检查(自动参考计数)

如何检查我的程序是否在ARC(自动参考计数)设备中运行?

我可以使用这段代码:

#if ! __has_feature(objc_arc)
#    define FMDBRelease(__v) ([__v release]);
#else
#    define FMDBRelease(__v)
#endif
Run Code Online (Sandbox Code Playgroud)

但这项工作在编译时.一个简单的代码是:

if ([object respondsToSelector:@selector(release)]) {
    [object release];
}
Run Code Online (Sandbox Code Playgroud)

但不行.我在任何地方都找不到它.有一个解决方案吗?

objective-c ios4 automatic-ref-counting

1
推荐指数
1
解决办法
1932
查看次数

通过选择器比较两个字符串:意外结果

我正在练习如何在Objective-C中使用选择器.
在这段代码中,我试图比较两个字符串:

int main (int argc, const char * argv[])
{
    @autoreleasepool
    {
        SEL selector= @selector(caseInsensitiveCompare:);
        NSString* str1=@"hello";
        NSString* str2=@"hello";
        id result=[str1 performSelector: selector withObject: str2];
        NSLog(@"%d",[result boolValue]);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但它打印为零.为什么?

编辑:
如果我将str2更改为@"hell",我会得到一个EXC_BAD_ACCESS.

objective-c selector automatic-ref-counting

1
推荐指数
1
解决办法
1144
查看次数

ARC [rewriter] NSInvocation的setArgument与不具有__unsafe_unretained的所有权的对象一起使用是不安全的

我一直把我的项目转换成ARC,我坚持这个错误.

与对象,与调用和&callerToRetain正显示出我的错误"[重写] NSInvocation的的setArgument是不是安全,可以使用一个对象,具有比其他__unsafe_unretained所有权"

+ (void)performSelector:(SEL)selector onTarget:(id *)target withObject:(id)object amount:(void *)amount callerToRetain:(id)callerToRetain{if ([*target respondsToSelector:selector]) {
    NSMethodSignature *signature = nil;
    signature = [*target methodSignatureForSelector:selector];
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];

    [invocation setSelector:selector];

    int argumentNumber = 2;

    // If we got an object parameter, we pass a pointer to the object pointer
    if (object) {
        [invocation setArgument:&object atIndex:argumentNumber];
        argumentNumber++;
    }

    // For the amount we'll just pass the pointer directly so NSInvocation will call the method using the number itself rather than a pointer …
Run Code Online (Sandbox Code Playgroud)

nsinvocation automatic-ref-counting

1
推荐指数
1
解决办法
1736
查看次数

使用ARC将指针传递给NSArray内的对象

我正在转换为ARC,并且在没有破坏代码的情况下找不到任何方法来摆脱这个编译器错误.

我需要将2个对象传递给选择器,因此我将它们都添加到数组中,如下所示,并将该数组发送到选择器:

LWPagerContent* content = nil;
NSArray* args = [NSArray arrayWithObjects:article, [NSValue valueWithPointer:&content], nil];

[self performSelectorOnMainThread:@selector(initArticlePagerContent:) withObject:args waitUntilDone:YES];
Run Code Online (Sandbox Code Playgroud)

此方法访问数组,但需要在数组中引用的地址处修改对象.它应该如图所示初始化LWPagerContent对象,然后该对象将由调用performSelector的原始方法使用.

- (void)initArticlePagerContent:(NSArray*)args {
Article* article = [args objectAtIndex:0];

LWPagerContent** contentPtr = [[args objectAtIndex:1] pointerValue];
*contentPtr = [[NSClassFromString([StyleSheet articleContentClass]) alloc] initWithArticle:article];
Run Code Online (Sandbox Code Playgroud)

}

这工作正常,但当我尝试转换为ARC时,我立即被告知"Implicit conversion of a non-Objective-C type 'void *' to 'LWPagerContent *__strong *' is disallowed with ARC".在以"LWPagerContent **..."(上面显示的第二个到最后一个)开头的行上发生此错误.我尝试过使用__bridge__autoreleasing其他帖子中的推荐,但似乎没有任何效果.任何建议都非常感谢.

pointers bridge reference objective-c automatic-ref-counting

1
推荐指数
1
解决办法
439
查看次数

ARC已启用.为什么'dealloc'方法不调用?

ARC已启用.为什么'dealloc'方法不调用?我应该在这里使用@autoreleasepool吗?为什么?谢谢.

@interface Test : NSObject
@end

@implementation Test

+(id)testFromNothing
{
  id res = [Test alloc];
  return res;
}

-(void)dealloc
{
  NSLog(@"deallocated");
}

@end


int main(int argc, const char * argv[])
{
  Test* test = [Test testFromNothing];
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

memory objective-c automatic-ref-counting

1
推荐指数
1
解决办法
270
查看次数

CFStringRef到NSString ARC泄漏.为什么?

我一直在四处寻找正确的方法,从去CFStringRefNSString在ARC以避免内存泄漏和一些主要的投回答提示:

NSString * string = (__bridge NSString *)cfString;
Run Code Online (Sandbox Code Playgroud)

我在这里使用这种方法,但在分析应用程序时,我仍然在这个小方法中得到内存泄漏[见附图].

所以,我不知道如何解决这个问题.任何人都有解决这个问题的方法吗?

谢谢

在此输入图像描述

在此输入图像描述

所以,显然CFRelease(ext)在返回之前添加了修复泄漏.问题是我不认为我完全理解原因.我以为这行:

NSString * extension = (__bridge NSString*)ext
Run Code Online (Sandbox Code Playgroud)

将取得Core Foundation ext字符串的所有权并处理该版本.任何人都可以解释这里到底发生了什么吗?

memory-leaks nsstring ios automatic-ref-counting

1
推荐指数
1
解决办法
2606
查看次数