我仍然是objective-c中的块的新手,并想知道我的伪代码是否正确.我不确定是否只是删除观察者或者我必须调用removeObserver:name:object:
-(void) scan {
Scanner *scanner = [[Scanner alloc] init];
id scanComplete = [[NSNotificationCenter defaultCenter] addObserverForName:@"ScanComplete"
object:scanner
queue:nil
usingBlock:^(NSNotification *notification){
/*
do something
*/
[[NSNotificationCenter defaultCenter] removeObserver:scanComplete];
[scanner release];
}];
[scanner startScan];
}
Run Code Online (Sandbox Code Playgroud)
更新:我EXC_BAD_ACCESS从这个区块收到间歇性,所以这不可能是正确的.
我有一个.xib文件包含一个UIView和2个UILabel子视图链接到一个名为Note的类,其中适当地分配给每个标签的出口,该类的定义包含以下内容.
@interface Note : UIView {
IBOutlet UILabel *time;
IBOutlet UILabel *content;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下代码构建它
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:@"Note" owner:self options:nil];
note = [nibViews lastObject];
[self addSubview:note];
Run Code Online (Sandbox Code Playgroud)
现在,在我的Note类dealloc阶段,我没有发布任何时间或内容,但我想知道我是否应该?
- (void)dealloc {
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)
我假设我没有,因为我没有在我的代码中的任何地方明确地保留这些对象,并且我没有将它们合成到getter/setter中.但是我对nib unarchiving知道我是否应该在我的dealloc阶段释放这些内容还不够了解?
大多数Apples文档似乎都避免使用自动释放的对象,尤其是在创建gui视图时,但我想知道使用自动释放对象的成本是多少?
UIScrollView *timeline = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 20, 320, 34)];
[self addSubview:timeline];
[timeline release];
Run Code Online (Sandbox Code Playgroud)
最终我应该使用一个策略,其中所有内容都是自动释放的,并且使用retain/release应该是特定情况的规则的例外吗?或者我通常应该使用自动释放的retain/release是来自[NSString stringWithEtc ...]等便利方法的返回对象的例外吗?
我整晚都在与Android性能作斗争并且可能解决了我一直在处理的问题,但是我仍然很困惑并且可以使用一些帮助.考虑这两个样本之间的时间差异.
第一个示例加载到可绘制的位图中并创建它的可变副本
Bitmap cacheBitmap;
Canvas cacheCanvas;
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
if (cacheBitmap != null) {
cacheBitmap.recycle();
}
Resources res = getContext().getResources();
Bitmap blankImage = BitmapFactory.decodeResource(res, R.drawable.blank);
/* copy existing bitmap */
cacheBitmap = Bitmap.createScaledBitmap(blankImage, w, h, false);
/* copy existing bitmap */
cacheCanvas = new Canvas();
cacheCanvas.setBitmap(cacheBitmap);
cacheCanvas.drawRGB(255, 255, 255);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(cacheBitmap, 0, 0, null); // draws in 7-8 ms
}
Run Code Online (Sandbox Code Playgroud)
第二个示例创建一个新的位图而不复制原始空白图像.
Bitmap cacheBitmap;
Canvas cacheCanvas;
protected void …Run Code Online (Sandbox Code Playgroud) 有谁知道苹果公司正在使用什么apI的Get Info面板来确定Lion的免费空间?我试图获得Apple报告的相同可用空间的所有代码都失败了,甚至Quick Look也没有显示Get Info显示的相同空间.如果我删除一堆文件并尝试读取可用空间,这似乎就会发生.

当我使用NSFileManager - > NSFileSystemFreeSize时,我得到42918273024字节
当我使用NSURL - > NSURLVolumeAvailableCapacityKey时,我得到42918273024字节
当我使用statfs - > buffer.f_bsize*buffer.f_bfree时,我得到43180417024字节
statfs获得与Quick Look类似的结果,但我如何匹配Get Info?
谁能解释为什么重新绘制到屏幕外的CGLayer会导致渲染随时间推移而变慢?让我向您展示我为说明问题而创建的测试。
@implementation DrawView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
//setup frame rate monitoring
fps = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
fps.textColor = [UIColor whiteColor];
fps.font = [UIFont boldSystemFontOfSize:15];
fps.text = @"0 fps";
[self addSubview:fps];
frames = 0;
lastRecord = [NSDate timeIntervalSinceReferenceDate];
//create a cglayer and draw the background graphic to it
CGContextRef context = UIGraphicsGetCurrentContext();
cacheLayer = CGLayerCreateWithContext(context, self.bounds.size, NULL);
CGImageRef background = [[UIImage imageNamed:@"background.jpg"] CGImage];
CGContextRef cacheContext = CGLayerGetContext(cacheLayer);
CGContextDrawImage(cacheContext, CGRectMake(0, 0, 768, 1024), …Run Code Online (Sandbox Code Playgroud)