标签: automatic-ref-counting

是否可以组合非ARC和ARC项目代码?

是否可以将ARC和非ARC项目结合起来?我还没有真正尝试过,但这是场景:

我们有一个带有标签控制器的旧iOS项目(非ARC).选项卡控制器中有一个选项卡,它没有任何功能或视图.我正在创建一个与旧项目相关的新项目,如果可以将我的新项目与旧项目相结合并将新项目的视图分配给选项卡,我希望它使用ARC.

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

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

ARC,桥接演员和GHUnit

我正在关注http://gabriel.github.com/gh-unit/docs/appledoc_include/guide_testing.html上的教程.问题是我的项目使用ARC而GHUnit没有.我管理过以前的错误,但现在我应该做桥接演员,我从未使用过,而且我迷路了.

NSString *string1 = @"a string";
GHAssertNotNULL(string1, nil); //error here
Run Code Online (Sandbox Code Playgroud)

错误描述:将Objective-C指针类型'NSString*'隐式转换为C指针类型'const void*'需要桥接转换.

任何帮助欢迎:)

objective-c gh-unit automatic-ref-counting

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

通过弱指针分配到块中的ivar

isFinished我的界面文件中有一个只读属性:

typedef void (^MyFinishedBlock)(BOOL success, NSError *e);

@interface TMSyncBase : NSObject {
     BOOL isFinished_;
}

@property (nonatomic, readonly) BOOL isFinished;
Run Code Online (Sandbox Code Playgroud)

我想YES稍后在某个块中将它设置为一个块,而不创建一个保留周期self:

- (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock {
    __weak MyClass *weakSelf = self;
    MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
        [weakSelf willChangeValueForKey:@"isFinished"];
        weakSelf -> isFinished_ = YES;
        [weakSelf didChangeValueForKey:@"isFinished"];
        theFinishedBlock(success, e);
    };

    self.finishedBlock = finishedBlockWrapper; // finishedBlock is a class ext. property
}
Run Code Online (Sandbox Code Playgroud)

我不确定这是正确的做法.这段代码会泄漏,破坏,还是没问题?也许有一种我更容易被忽视的方式?

objective-c objective-c-blocks ivar automatic-ref-counting declared-property

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

Objective C/iOS:使用ARC(内存泄漏)释放内存

我是新手iOS/Objective-C,我不理解正确释放内存.为了测试它,我创建了一个空的ARC启用iPhone-Project并创建了一个非常简单的测试类:

#import "MemTest.h"

@implementation MemTest {

}

-(void) start {
    for (int i = 0; i < 1500000; i++) {
        NSMutableString *myString = [NSMutableString string];

        // The appended string is 2000 characters long in the real test class.
        [myString appendString:@"12345678901234567890123456 <very long>"];

        if (i % 1000 == 0) {
            NSLog(@"i = %d", i);
        }

        myString = nil;
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

我只是开始测试AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MemTest *test = [[MemTest alloc] init];
    [test …
Run Code Online (Sandbox Code Playgroud)

memory-leaks objective-c ios automatic-ref-counting

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

iOS - 开始iOS教程 - 变量之前的下划线?

我开始学习如何在一分钟内开发一些iOS,而我目前正在开始使用我正在开发的IOS6书.

在第二章中,有一个简单的教程,显示两个按钮和一个标签,按下按钮时,标签上会显示按下一个按钮.

我已经完成了教程,但它提出了一个我无法找到答案的问题.

本教程使用ARC(自动引用计数),以防万一.

这是代码,

头文件:

#import <UIKit/UIKit.h>

@interface MTMViewController : UIViewController


@property (weak, nonatomic) IBOutlet UILabel *statusLabel;


- (IBAction)buttonPressed:(UIButton *)sender;

@end
Run Code Online (Sandbox Code Playgroud)

和m文件:

#import "MTMViewController.h"    

@implementation MTMViewController

- (IBAction)buttonPressed:(UIButton *)sender {


    NSString *title = [sender titleForState:UIControlStateNormal];

    NSString *plainText = [NSString stringWithFormat:@"%@ button pressed.", title];

    statusLabel.text = plainText;


}

@end
Run Code Online (Sandbox Code Playgroud)

以上是它出现在书中的方式,但是当在Xcode中进行教程时,我无法使用以下行编译:

statusLabel.text = plainText;
Run Code Online (Sandbox Code Playgroud)

而不得不改为:

_statusLabel.text = plainText;
Run Code Online (Sandbox Code Playgroud)

当我完成这个代码编译并运行良好,我试图找出为什么发生这种情况,通过回顾教程,看看我错过了什么,但我没有看到任何东西.

任何人都可以解释为什么书中的代码没有编译,为什么我必须在变量的前面添加下划线?这是正确的还是我做错了什么?

cocoa-touch objective-c ios automatic-ref-counting ios6

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

Objective-C中的私有变量是否强大?

所以搜索Stack Overflow这似乎是如何在Objective-C中创建私有变量:

@interface ClassName()
{
@private
    NSArray* private;
}
@property (strong, nonatomic) NSArray* public;
@end
Run Code Online (Sandbox Code Playgroud)

现在这是我感到困惑的地方.该属性声明为(strong, nonatomic),但私有变量没有任何类型.那么弧怎么知道它是否强大?

iphone xcode objective-c private-members automatic-ref-counting

7
推荐指数
2
解决办法
2961
查看次数

iOS视图控制器内存在被解除后未释放

当用户单击按钮时,它会显示一个带有两个视图控制器的新标签栏视图控制器.我就是这样做的

ACLevelDownloadController *dvc = [[ACLevelDownloadController alloc] initWithNibName:@"ACLevelDownloadController" bundle:[NSBundle mainBundle]];
ACInstalledLevelsController *ivc = [[ACInstalledLevelsController alloc] initWithNibName:@"ACInstalledLevelsController" bundle:[NSBundle mainBundle]];
UITabBarController *control = [[UITabBarController alloc] init];
control.viewControllers = @[dvc, ivc];
dvc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0];
ivc.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
[self presentViewController:control animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

这很好用.我关闭那些视图控制器具有dismiss在两个方法ACLevelDownloadController和ACInstalledLevelsController.这也很好.奇怪的是,当我呈现视图控制器时,内存使用率会上升 在此输入图像描述

但它永远不会失败.如果我再次呈现它,它会更上升 在此输入图像描述 我正在使用ARC.为什么视图控制器使用的内存在被解除后不被释放?

编辑

他们被解雇的方式是两者ACLevelDownloadController并且ACInstalledLevelsController已经连接了IBActions,当他们被点击时调用这个方法

- (void)dismiss:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud)

memory objective-c ios automatic-ref-counting

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

当块在初始化程序中时捕获块中的变量

考虑一下:

id observer = [[NSNotificationCenter defaultCenter] 
    addObserverForName:MyNotification 
                object:nil 
                 queue:nil 
            usingBlock:^(NSNotification *note) {
                [[NSNotificationCenter defaultCenter] 
                        removeObserver:observer 
                                  name:MyNotification 
                                object:nil
            ];
            // do other stuff here...
    }
];
Run Code Online (Sandbox Code Playgroud)

我正在使用此模式观察一次通知,然后停止观察它.但是LLVM告诉我(在ARC下)当被块捕获时,变量'观察者'是未初始化的.

我怎么能解决这个问题,因为块必须在初始化之前捕获变量,它是初始化器的一部分?是否会使用__block限定符observer来做诀窍?

objective-c compiler-warnings objective-c-blocks automatic-ref-counting

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

CG栅格数据 - CGProviderCreateWithCopyOfData - 无法释放?

我试图找到内存泄漏的来源,详见此处:

无法发布Quartz 2D和Core Text创建的图像

当我使用乐器时,我可以看到有一种名为CG Raster Data的东西,其尺寸稳步增长,从未发布过.

有谁知道可能导致这种情况的原因以及如何补救?看起来这可能是某种图像缓存,但是可以从代码中清除它吗?

objective-c quartz-2d core-text ios automatic-ref-counting

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

更改rootviewcontroller导致内存泄漏

在询问我自己的问题之前,我检查了所有的问题,但它们都不适合我.

当我在第一次运行时打开我的应用程序时,我调用了FirstViewController(它包含两个按钮:注册或登录).如果用户选择注册,我会带上RegisterViewController(它包含一个按钮,让用户登录以防他改变主意).最后一个LoginViewController(还包含一个打开注册屏幕的按钮)

FirstViewController中两个按钮的代码

- (IBAction)signupPressed:(id)sender {
    AppDelegate *app = (AppDelegate*) [UIApplication sharedApplication].delegate;
    app.window.rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"RegisterViewController"];
}

- (IBAction)loginPressed:(id)sender {
    AppDelegate *app = (AppDelegate*) [UIApplication sharedApplication].delegate;
    app.window.rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
}
Run Code Online (Sandbox Code Playgroud)

RegisterViewController按钮的代码,通向LoginViewController(我尝试过其中一个解决方案,但失败了)

- (IBAction)loginPressed:(id)sender {
    AppDelegate *app = (AppDelegate*) [UIApplication sharedApplication].delegate;


    UIViewController *previousRootViewController = app.window.rootViewController;

    app.window.rootViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];

    // Nasty hack to fix http://stackoverflow.com/questions/26763020/leaking-views-when-changing-rootviewcontroller-inside-transitionwithview
    // The presenting view controllers view doesn't get removed from the window as its currently transistioning and presenting a view controller
    for (UIView *subview in …
Run Code Online (Sandbox Code Playgroud)

memory-leaks ios automatic-ref-counting

7
推荐指数
0
解决办法
1084
查看次数