【iOS】Autosynthesized property警告解决方案

来源:互联网 发布:公众号小说收费源码 编辑:程序博客网 时间:2024/06/06 09:19
warning:
Autosynthesized property 'myTimer' will use synthesized instance variable '_myTimer', not existing instance variable 'myTimer'.
我所有在@synthesize 里的变量或者说在@property里面的变量都有这个警告。

在我的.h 文件中我声明了一个成员变量NSTimer *myTimer,同时声明属性

#import <UIKit/UIKit.h>@interface ViewController : UIViewController{    NSTimer *myTimer;}@property (weak,nonatomic) NSTimer *myTimer;@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *mActivityIndicatorView;@property (weak, nonatomic) IBOutlet UIProgressView *progressView;- (IBAction)startToMove:(id)sender;- (IBAction)downloadProgress:(id)sender;@end

并在.m file文件未合成属性时出现


和成熟性 出现如下问题



解决方案:

使用@property定义变量ivar无非三种情况
1)没有合成@synthesized ,则系统会通过Autosynthesized合成一个_ivar
2)如果使用@synthesized ivar;则声称的变量为ivar
3)如果使用@synthesized ivar = _ivar;则声明的变量为_ivar。

如果使用self.ivar则三种定义方法都可以,因为self.ivar是调用的getter方法。

最后,目前Apple推荐的定义方法是第3)种。而且无需在@interface里面再定义变量

最后.m文件

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize myTimer=_myTimer;- (void)viewDidLoad{    [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}- (IBAction)startToMove:(id)sender {    if ([self.mActivityIndicatorView isAnimating]) {        [self.mActivityIndicatorView stopAnimating];    }else{        [self.mActivityIndicatorView startAnimating];    }}- (IBAction)downloadProgress:(id)sender {    myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(download) userInfo:nil repeats:YES];}- (void)download{    self.progressView.progress=self.progressView.progress+0.1;        if (self.progressView.progress==1.0) {        [myTimer invalidate];        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"download completed!" message:@" " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];        [alert show];    }}@end


0 0
原创粉丝点击