我創建了一個Food
對象,我希望能夠設置和獲取屬性(如time
)。出於某種原因,我被允許設置屬性,但我無法獲得get屬性。我簡單地調用food.time
簡單的類示例@property不允許屬性檢索
Property 'time' not found on object of type 'conts __strong id'
我不知道,如果問題是把它在,然後從一個數組中檢索,或者如果它是我的對象類是如何定義收到以下錯誤。在這個例子中,我簡化了它,以便您可以看到我如何使用它。
有些控制器(與其他方法這裏沒有顯示)
#import "Food.h"
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *foodArray = @[firstFood];
for (id food in foodArray) {
UILabel *foodLabel = [[UILabel alloc]
initWithFrame:CGRectMake(10, 180, self.view.frame.size.width-20, 50)];
foodLabel.backgroundColor = [UIColor clearColor];
foodLabel.text = food.time; // This line causes error
foodLabel.textColor = [UIColor blackColor];
[foodLabel setFont:[UIFont fontWithName:@"Courier" size:14]];
[self.view addSubview:foodLabel];
}
}
Food.h
#import <Foundation/Foundation.h>
@interface Food : NSObject
@property (strong, nonatomic) NSString *time;
@property (strong, nonatomic) NSString *title;
@property (strong, nonatomic) NSString *description;
@property (strong, nonatomic) NSString *place;
@end
Food.m
#import "Food.h"
@implementation Food
@end
你需要在這裏輸入一個類型foodLabel.text =(Food *)food.time; – Pawan