小编Kos*_*s.N的帖子

暂停GCD查询问题

我无法暂停gcd查询.以下是一些演示此问题的代码:

static dispatch_queue_t q=nil;

static void test(int a){
    if(q){
        dispatch_suspend(q);
        dispatch_release(q);
        q=nil;
    }
    q=dispatch_get_global_queue(0,0);
    dispatch_async(q,^ {
        while(1){NSLog(@"query %d",a);sleep(2);}
    });

}

int main(int argc, const char* argv[]){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    test(1);

    //blah blah blah

    test(2);

    while(1){}
    [pool release];
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试做的是在第二次调用函数测试时暂停,释放和重新初始化查询q,但显然我的代码是错误的,并且查询q的两个实例都继续运行.

非常感谢您的帮助,谢谢.

objective-c grand-central-dispatch

30
推荐指数
2
解决办法
3万
查看次数

实例变量问题的__block属性.

编译Objective-C类时遇到以下错误:

VideoView.h:7: error: __block attribute can be specified on variables only
Run Code Online (Sandbox Code Playgroud)

这里也是头文件的重要部分:

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface VideoView :UIView{
@private
    __block AVPlayer *player;   
}
...
Run Code Online (Sandbox Code Playgroud)

有没有解释为什么g ++认为我在非变量对象上应用__block属性?

objective-c ios objective-c-blocks

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

使对象无效的Objective-C方法

我在 Objective-C 中编写一个使对象为零的方法时遇到了一些麻烦。下面是一些例子:

@interface testA : NSObject
{
     NSString *a;
}

@property (nonatomic, retain) NSString *a;

+(testA*)initWithA:(NSString *)aString;

-(void)displayA;
-(void)nillify;
@end

@implementation testA
@synthesize a;
+(testA*)initWithA:(NSString *)aString{
    testA *tst=[[testA alloc] init];
    tst.a=aString;
    return [tst autorelease];
}
-(void)displayA{
    NSLog(@"%@",self.a);    
}
-(void)nillify{
    self=nil;
}
- (void)dealloc {
    [a release];
    [super dealloc];
}
@end

int main(int argc, char **argv){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    testA *test=[testA initWithA:@"some test"];
    [test displayA];
    test=nil;
    //[test nillify];

    NSLog(@"after setting to nil");

    [test displayA];
    [pool release];
    return …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c

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