block传值时遇到的问题

来源:互联网 发布:线切割3b编程实例刀 编辑:程序博客网 时间:2024/06/13 00:34

#import "ViewController.h"

#import "MyOperation.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    NSOperationQueue*deque=[[NSOperationQueuealloc]init];

   UIImageView*imageview=[[UIImageViewalloc]initWithFrame:self.view.frame];

    [self.viewaddSubview:imageview];

    

   MyOperation*operation3=[[MyOperationalloc]init];

    operation3.str=@"http://news.xinhuanet.com/world/2015-10/22/128344315_14454681824761n.jpg";


-------关于代码块传值,遇到了两次崩掉了原因都是self.myBlock(image)崩掉了,这个问题的解决方法:self.block(参数)所在的方法写在block赋值的后面才行,绝不能写在前面,否则就会崩;block的使用顺序为 :声明——赋值——引用,引用一定要在赋值的后面,否则就会引发错误。-----------------

    operation3.myBlock=^void(UIImage*image){

        imageview.image=image;

    };

    [operation3one];

    [dequeaddOperation:operation3];

    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end

----------//  MyOperation.h-----------------


#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface MyOperation :NSOperation

@property(nonatomic,copy)void(^myBlock)(UIImage*);

@property(nonatomic,copy)NSString*str;

-(void)one;

@end

-------------//  MyOperation.m---------------

#import "MyOperation.h"


@implementation MyOperation

-(void)one;

{

    NSData*data=[NSDatadataWithContentsOfURL:[NSURLURLWithString:self.str]];

   UIImage*image=[UIImageimageWithData:data];

   self.myBlock(image);

}

@end













0 0
原创粉丝点击