[英]Objective C Property Syntax
我对我的代码有快速提问:
这是我的Animal.h头文件:
#import <Foundation/Foundation.h>
@interface Animal : NSObject
@property (nonatomic) int age;
@property (nonatomic, strong) NSString *name;
@property (nonatomic,strong) NSString *breed;
@property (retain, nonatomic) UIImage *image;
-(void) bark;
-(void)barkNumTimes: (int)numOfTimesToBark;
-(void)barknumTimes:(int)numberOfTimes loudly:(bool) isLoud;
-(int) ageInDogYears: (int)humanYears;
@end
由于某种原因在线:
@property (retain, nonatomic) UIImage *image;
我得到一个错误,说“具有'保留(或强)'属性的属性必须是对象类型”。
我的ViewController.m类是我创建三个Animal对象的地方,并使用我在Animal.h中创建的UIImage属性,并将每个Animal对象UIImage属性设置为我在支持文件中的某个图像:
#import "ViewController.h"
#import "Animal.h"
#import "Puppy.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.whichDog = 0;
Animal *whiteDog = [[Animal alloc]init];
[whiteDog setName:@"white"];
[whiteDog setBreed:@"White Dog"];
whiteDog.image = [UIImage imageNamed:@"whitedog.jpeg"];
Animal *brownDog = [[Animal alloc] init];
[brownDog setName:@"brown"];
[brownDog setBreed:@"Brown Dog"];
brownDog.image = [UIImage imageNamed:@"browndog.jpeg"];
Animal *husky = [[Animal alloc] init];
[husky setName:@"husky"];
[husky setBreed:@"Husky Dog"];
husky.image = [UIImage imageNamed:@"husky.jpeg"];
self.myAnimals = [[NSMutableArray alloc] init];
[self.myAnimals addObject:whiteDog];
[self.myAnimals addObject:brownDog];
[self.myAnimals addObject:husky];
Puppy *pup = [[Puppy alloc]init];
[pup setName:@"coby"];
[pup setBreed:@"Portuguese Water Dog"];
pup.image = [UIImage imageNamed:@"puppy.jpeg"];
}
- (IBAction)newDogBarButton:(UIBarButtonItem *)sender{
int numOfDogs = (int)[self.myAnimals count];
int randomIndex = arc4random() % numOfDogs;
Animal *randomAnimal = [self.myAnimals objectAtIndex:randomIndex];
[UIView transitionWithView:self.view duration:1.5 options:UIViewAnimationOptionTransitionCurlDown animations:^{
self.imageView.image = randomAnimal.image;
self.carNAme.text = randomAnimal.name;
self.extra.text = [randomAnimal breed];
} completion:^(BOOL finished) {
}];
sender.title = @"And Another";
self.whichDog = randomIndex;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
出于某些原因,在Animal.h中,我不断收到“带有'保留(或强)'属性的属性必须是对象类型”的错误。 我不太确定这些属性中的保留或强大含义,但是有人可以向我解释我在代码中做错了什么。 非常感谢你的帮助。
UIImage属于UIKit,因此导入UIKit而不是Foundation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.