Objective C Property Syntax

时间:2015-06-25 19:11:01

标签: objective-c

I have quick question regarding my code: This is my Animal.h header file: #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 For some reason at the line: @property (retain, nonatomic) UIImage *image; I get an error saying that "Property with 'retain (or strong)' attribute must be of object type". My ViewController.m class is where I created three Animal objects and used the UIImage property which I created in Animal.h and set each of the Animal objects UIImage property to a certain image I have in my supporting files: #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 For some reason in Animal.h I keep getting that error which says "Property with 'retain (or strong)' attribute must be of object type". I am not very sure on what this retain or strong means in the properties, but can someone please explain to me what I am doing wrong in my code. Thank you so much for the help.

1 个答案:

答案 0 :(得分:3)

UIImage belongs to UIKit, so import UIKit instead of Foundation