Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 目标C属性语法_Objective C - Fatal编程技术网

Objective c 目标C属性语法

Objective c 目标C属性语法,objective-c,Objective 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, no

我有一个关于我的代码的快速问题:

这是我的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
我得到一个错误,指出“具有'retain(或strong)'属性的属性必须是对象类型”

在我的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中,我不断得到这样一个错误,即“具有'retain(或strong)'属性的属性必须是对象类型”。我不太清楚这个retain或strong在属性中意味着什么,但是有人能解释一下我在代码中做错了什么吗。非常感谢您的帮助。

UIImage属于UIKit,因此导入UIKit而不是Foundation

是否使用ARC?你为什么把
strong
retain
与你的房产混在一起?嗨,我实际上不知道strong和retain是什么意思,这就是为什么我不知道该用哪一个。在引入ARC之前,我们通常使用
retain
。我们现在在ARC中使用
strong
。因此,用
strong
@ClayHorder替换
retain
,您使用的是指南还是教程?因为它可能是过时的。所以在上面添加另一个导入语句:UIKit导入“uiKIT”?不,用<代码> >导入导入< /COD>(或<代码> @导入UIKIT;< /COD>)替换“<代码>导入> <代码>。用UIKit替换基础,UIKit包含基础框架。您可能会使用语法
@import-UIKit好的。我在工作中得到了密码。谢谢你的帮助!哦,所以我觉得我甚至不需要:#导入。
#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