Objective C - IOS - 在类型的对象上找不到属性 - 访问对象变量时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19448040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective C - IOS - Property not found on object of type - when accessing object variables
提问by Dancer
I'm building a UIView which is connected to a UITableView containing a list of shop items and populated by an array of data which is associated to an object class (shopObjects).
我正在构建一个 UIView,它连接到包含商店项目列表的 UITableView,并由与对象类 (shopObjects) 关联的数据数组填充。
here is my shop objects H file -
这是我的商店对象 H 文件 -
#import <Foundation/Foundation.h>
@interface shopObjects : NSObject
@property(strong) NSString *shopITitle;
@property(strong) NSString *shopIGroup;
@property(strong) NSString *shopIDesc;
@property(strong) NSString *shopIPrice;
@property Boolean offer;
-(id)initWithshopITitle:(NSString *)shopITitleD shopIGroup:(NSString *)shopIGroupD shopIDesc: (NSString *)shopIDescD shopIPrice:(NSString *)shopIPriceD offer:(Boolean )offerD;
@end
Shop Object . M file
商店对象。M档
#import "shopObjects.h"
@implementation shopObjects
-(id)initWithshopITitle:(NSString *)shopITitleD shopIGroup:(NSString *)shopIGroupD shopIDesc:(NSString *)shopIDescD shopIPrice:(NSString *)shopIPriceD offer:(Boolean )offerD{
self= [super init];
if(self){
self.shopITitle = shopITitleD;
self.shopIGroup = shopIGroupD;
self.shopIDesc = shopIDescD;
self.shopIPrice = shopIPriceD;
self.offer = (offerD);
}
return self;
}
@end
this is my view controller .h file -
这是我的视图控制器 .h 文件 -
#import <UIKit/UIKit.h>
#import "shopObjects.h"
@interface shopVCSelectionViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIScrollView *shopResScroller;
@property (weak, nonatomic) IBOutlet UILabel *ShopItemTitle;
@property (weak, nonatomic) IBOutlet UITextView *ShopItemDesc;
@property (weak, nonatomic) IBOutlet UILabel *ShopItemPrice;
@end
and VC .m file -
和 VC .m 文件 -
#import "shopVCViewController.h"
@interface shopVCViewController ()
@end
@implementation shopVCViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Set Scroller
[self.shopScroll setScrollEnabled:YES];
[self.shopScroll setContentSize:CGSizeMake(320, 1100)];
self.title = @"Shop";
self.shopArray = [NSArray arrayWithObjects:@"1 x PT session - £25",@"3 for 2 PT sessions - £50 ",@"1 x Running Club - £15",@"1 x PT session", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [self.shopArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =@"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text=[self.shopArray objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
I am trying to link up the labels/text fields with the relevant object variables - but the shopObjects object / associated variables dont appear in the predictive text and I get the following property not found error - I've no idea why! Can anyone offer some advice please?
我正在尝试将标签/文本字段与相关对象变量联系起来 - 但 shopObjects 对象/关联变量没有出现在预测文本中,并且我收到以下属性未找到错误 - 我不知道为什么!任何人都可以提供一些建议吗?
采纳答案by Simon McLoughlin
You did not inherit shopObjects
in your viewController. It is a separate object.You need to create a variable of type shopObjects
, set its properties and then call that. e.g.
您没有shopObjects
在 viewController 中继承。它是一个单独的对象。您需要创建一个 type 变量shopObjects
,设置其属性,然后调用它。例如
shopVcSelectionViewController.h
@property (strong, nonatomic) shopObjects *shopObject;
shopVcSelectionViewController.m
...
self.shopItemTitle.text = self.shopObject.shopTitle
...
回答by Duncan C
The code you posted is in an instance method of the class shopSelectionViewController. A shopSelectionViewController has a property shopItemTitle, but not a shopITitle property.
您发布的代码位于类 shopSelectionViewController 的实例方法中。shopSelectionViewController 有一个属性 shopItemTitle,但没有一个 shopITitle 属性。
This line
这条线
self.shopItemTitle.text = self.shopITitle;
Does not make sense.
没有意义。
If you are trying to fetch a title from an object of class "shopObjects", then you would need something like this:
如果您试图从“shopObjects”类的对象中获取标题,那么您需要这样的东西:
shopObjects *someShopObject = //code to fetch a shopObject
self.shopItemTitle.text = someShopObject.shopITitle;
回答by Kayce Basques
You'll also see this error if you have an object instance which is the same name as a class, and you mistakenly operate on the class, not your instance (thanks, Xcode auto-complete).
如果您有一个与类同名的对象实例,并且您错误地操作了该类,而不是您的实例(感谢 Xcode 自动完成),您也会看到此错误。
For example, suppose that you have a view controller SetSignsViewController
and you create an instance of the view controller called setSignsViewController
:
例如,假设您有一个视图控制器SetSignsViewController
并创建了一个名为 的视图控制器实例setSignsViewController
:
SetSignsViewController *setSignsViewController =
(SetSignsViewController *) uiNavigationController.topViewController;
You want to set the name of a property called openHouseLocation
:
您想设置名为 的属性的名称openHouseLocation
:
SetSignsViewController.openHouseLocation = self.openHouseLocation;
This yields Property 'openHouseLocation' not found on object of type 'SetSignsViewController'
because you're operating on SetSignsViewController
, not setSignsViewController
(first letter should be s
, not S
). What you really meant to do was:
这Property 'openHouseLocation' not found on object of type 'SetSignsViewController'
是因为您正在操作SetSignsViewController
,而不是setSignsViewController
(第一个字母应该是s
,不是S
)。你真正想做的是:
setSignsViewController.openHouseLocation = self.openHouseLocation;
回答by Tendulkar
Try to @synthesize
the variables in .m file of ShopObjects.
尝试使用@synthesize
ShopObjects 的 .m 文件中的变量。