【iOS】Autosynthesized property警告解决方案

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


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值