2015-06-25 115 views
1

我有一個關於我的代碼快速的問題:目標C屬性語法

這是我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.h創建的UIImage的屬性,並設置每個動物的對象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中,我不斷收到那個說「帶有'保留(或強)屬性的屬性必須是對象類型」的屬性。我不確定這些屬性中保留的或強大的手段,但是有人可以向我解釋我在代碼中做錯了什麼。十分感謝你的幫助。

+0

UIKit的?你爲什麼要將「強」和「保留」與你的房產混合? – JAL

+0

嗨,我實際上不確定什麼強大和保留手段,這就是爲什麼我很困惑哪一個使用。 – LP496

+0

在引入ARC之前,我們曾經使用'retain'。我們現在在ARC中使用'strong'。所以用'strong'替換'retain'。 – Rob

回答

2

的UIImage屬於UIKit的,所以您在使用ARC導入,而不是基金會

+0

因此,在頂部添加另一個像這樣的導入語句:#import「UIKit」? – LP496

+0

不,用'#import '(或'@import UIKit;')替換'#import '。 – Rob

+1

用UIKit替換Foundation,UIKit包含Foundation框架。您可以使用語法'@import UIKit;' – vadian