看完之后:
iphone:使用iOS 5和XCode 4.2提交应用程序?
我意识到使用ARC并期望使用这种技术将应用程序发布到应用程序商店可能对我来说有点短视.对我来说最好的行动方案是什么?
感谢您的建议.
更新:以防万一有人徘徊到这个,这是Xcode 4.2仍然处于测试阶段.最终结果:除非您等待最终版本,否则请勿使用测试版功能.
我是ARC的新手,我已经玩了不到一个星期.我想做的是非常基本的.我有一个显示按钮的视图控制器.单击按钮时,需要调用相应的选择器.但是,使用ARC,应用程序崩溃时出现EXC_BAD_ACCESS消息.下面是我的MainViewController的代码
- (void)loadView
{
[super loadView];
UIButton *testButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setFrame:CGRectMake(80,50,160,50)];
[testButton setTitle:@"Click Me" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:testButton];
}
-(void)buttonClicked:(id)sender
{
NSLog(@"Button Clicked");
}
Run Code Online (Sandbox Code Playgroud)
我启用了Zombie Objects,并且能够在调试日志中找到以下消息
2012-02-21 22:46:00.911 test[2601:f803] *** -[MainViewController performSelector:withObject:withObject:]: message sent to deallocated instance 0x6b4bba0
Run Code Online (Sandbox Code Playgroud)
看看上面的消息,我觉得ARC过早地发布了我的MainViewController.我不确定我在这里做错了什么.如果我错过了什么,请告诉我.
提前致谢
为了提供尽可能多的信息,这里是我正在做的事情的一个非常基本的例子
type
IMyInterface = interface
[THE_GUID_HERE]
// some methods
end;
TMyInterfaceArray = Array of IMyInterface;
TMyInterfacedObject = class(TInterfacedObject, IMyInterface)
// implementation of the Interface etc. here
end;
TContainingObject = class
private
FIObjectArray: TMyInterfaceArray;
public
constructor Create;
destructor Destroy; override;
procedure NewInstanceOfInterfacedObject;
end;
implementation
constructor TContainingObject.Create;
begin
inherited;
// Just to illustrate that an Instance is being created...
NewInstanceOfInterfacedObject;
end;
destructor TContainingObject.Destroy;
var
I: Integer;
begin
for I := Low(FIObjectArray) to High(FIObjectArray) do
FIObjectArray[I] := nil;
SetLength(FIObjectArray, 0); // Array collapsed …Run Code Online (Sandbox Code Playgroud) 在我的Cocoa项目中,我有一些地方,我使用malloc/free.然而几个月前我决定重构以利用ARC,为了做到这一点,我试图替换malloc,它将返回一个指针,指向将被自动清理的东西.
我使用了这个功能(错误检查和其他记录省略)
+ (void *) MallocWithAutoCleanup: (size_t) size
{
NSMutableData * mutableData = [[NSMutableData alloc] initWithLength:size];
void * data = [mutableData mutableBytes];
return data;
}
Run Code Online (Sandbox Code Playgroud)
这种方法运行良好一段时间,但最近出现了随机内存覆盖问题.我找到了这个函数的原因,看起来正在发生的是NSMutableData实例正被释放,即使我保持指向其mutableBytes的指针.
我猜这种情况正在发生,因为对象的唯一直接引用是本地的并且正在消失,并且mutableBytes指向对象内部,因此ARC不够智能来处理那种引用计数.
有没有什么办法可以重构这段代码来保留mutableData对象,只要使用了mutableBytes指针(即某人有引用它)?我知道一个选项就是返回NSMutableData本身,但这需要一些重构并且看起来非常混乱.
我已经看到了相同错误的类似问题.在将代码重构为Arc之后,我得到 Receiver类型'CGPointObject'作为实例消息是一个前向声明错误.建议将@class方法移动到.h文件和#import .h文件的声明,并明智地使用{.
我做了所有的建议,但我仍然得到错误.
CCParallaxNode-Extras.h
#import "cocos2d.h"
@class CGPointObject;
@interface CCParallaxNode (Extras)
-(void) incrementOffset:(CGPoint)offset forChild:(CCNode*)node;
@end
Run Code Online (Sandbox Code Playgroud)
CCParallaxNode-Extras.m
#import "CCParallaxNode-Extras.h"
#import "CCParallaxNode.h"
@implementation CCParallaxNode(Extras)
-(void) incrementOffset:(CGPoint)offset forChild:(CCNode*)node
{
for( unsigned int i=0;i < parallaxArray_->num;i++) {
CGPointObject *point = parallaxArray_->arr[i];
if( [[point child] isEqual:node] ) {
[point setOffset:ccpAdd([point offset], offset)];
break;
}
}
}
@end
Run Code Online (Sandbox Code Playgroud)
课程定义:CCParallaxNode.m
#import "CCParallaxNode.h"
#import "Support/CGPointExtension.h"
#import "Support/ccCArray.h"
@interface CGPointObject : NSObject
{
CGPoint ratio_;
CGPoint offset_;
CCNode *child_; // weak ref
}
@property (nonatomic,readwrite) CGPoint ratio; …Run Code Online (Sandbox Code Playgroud) 正如标题中所写,我有一个属性UITextField.我把它UITextField加到了UIView.
如果我有一个强大的指针,UITextField出现,
如果我有一个弱指针,UITextField则不会出现.
当我有一个弱指针时出了什么问题?我做了同样的UIButton事情,然后它实际上出现(强指针和弱指针).
这是一些代码:
CreateCategoryView.h
@interface CreateCategoryView : UIView
@property (weak, nonatomic) UITextField *nameTextField;
@end
Run Code Online (Sandbox Code Playgroud)
CreateCategoryView.m
@implementation CreateCategoryView
- (id)initWithFrame:(CGRect)frame andParent {
self = [super initWithFrame:frame];
if (self) {
self.nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(5, 30, 310, 25)];
self.nameTextField.borderStyle = UITextBorderStyleRoundedRect;
self.nameTextField.textColor = [UIColor blackColor];
self.nameTextField.backgroundColor = [UIColor whiteColor];
[self addSubview:self.nameTextField];
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud) 我知道ARC中的__block保留了变量.然后,在分配变量之前访问块内的变量时,可以使用此方法,如下所示:
__block __weak id observer = [[NSNotificationCenter defaultCenter] addObserverForName:MPMoviePlayerPlaybackDidFinishNotification object:player queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notif){
// reference the observer here. observer also retains this block,
// so we'd have a retain cycle unless we either nil out observer here OR
// unless we use __weak in addition to __block. But what does the latter mean?
}];
Run Code Online (Sandbox Code Playgroud)
但是我在解析这个问题时遇到了麻烦.如果__block导致观察者被块保留,那么有效地强弱都意味着什么呢?这是__weak做什么的?
我正在使用ARC和ios sdk 6.0.
我很确定我有一些内存泄漏,我无法追踪.
运行静态分析器后,我会收到有关以下两种方法的警告:
+ (id<MXURLRequest>) requestWithURL:(NSURL*)url {
MXASIURLRequest *request = [[MXASIURLRequest alloc] init];
[request setUrl:url];
return request; // STATIC ANALYSER: Potential leak of an object stored into 'request'
}
- (id)parseBody:(NSError *)error {
NSString *contentType = [[_request responseHeaders] objectForKey:@"Content-Type"];
id body = nil;
if ([contentType hasPrefix:@"application/json"] ||
[contentType hasPrefix:@"text/json"] ||
[contentType hasPrefix:@"application/javascript"] ||
[contentType hasPrefix:@"text/javascript"]) {
body = [NSJSONSerialization JSONObjectWithData:[_request responseData] options:NSJSONReadingMutableLeaves error:&error];
} else if ([contentType hasPrefix:@"image/"] ||
[contentType hasPrefix:@"audio/"] ||
[contentType hasPrefix:@"application/octet-stream"]) {
body = [_request …Run Code Online (Sandbox Code Playgroud) 对于许多类,有initXXX方法和typeXXX方法,例如:
NSNumber *n1 = [[NSNumber alloc] initWithInt:1];
NSNumber *n2 = [NSNumber numberWithInt:1];
Run Code Online (Sandbox Code Playgroud)
我读过有关手动内存管理的内容,我认为我理解这些内容与手动内存管理有何不同(第二种是自动释放对象的快捷方式).
但是使用ARC我不明白我应该使用哪种API?我是否应该只使用alloc-init模式,因为ARC足够智能,可以在必要时自动释放值,并在仅在本地使用值时避免自动释放开销?或者ARC可以优化自动释放和相应的开销,即使我使用像numberWithInt这样的东西?
我想在可用时使用第二种变体,因为它更短更容易阅读.但出于这个原因,我不想引入性能开销.
constructor initialization objective-c automatic-ref-counting
警告是:
存储在'escaped_value'中的对象的潜在泄漏
这是代码:
- (NSURL*)generateURL:(NSString*)baseURL params:(NSDictionary*)params {
if (params) {
NSMutableArray* pairs = [NSMutableArray array];
for (NSString* key in params.keyEnumerator) {
NSString* value = params[key];
NSString* escaped_value = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL, /* allocator */
(__bridge CFStringRef)value,
NULL, /* charactersToLeaveUnescaped */
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
[pairs addObject:[NSString stringWithFormat:@"%@=%@", key, escaped_value]];
}
NSString* query = [pairs componentsJoinedByString:@"&"];
NSString* url = [NSString stringWithFormat:@"%@?%@", baseURL, query];
return [NSURL URLWithString:url];
} else {
return [NSURL URLWithString:baseURL];
}
}
Run Code Online (Sandbox Code Playgroud) ios ×7
objective-c ×7
xcode ×3
iphone ×2
memory ×2
arrays ×1
cocoa ×1
constructor ×1
delphi ×1
delphi-xe2 ×1
interface ×1