我正在尝试使用字符串,int和浮点数来为Objective C使用getter setter类。但是当涉及到执行时,它表明存在通过引用从该行传递的变量的错误访问:
[self setState:valueState];
Applpication Part:
CheckPoints *myCar=[[CheckPoints alloc] init];
[myCar setState:1];
[myCar setLatitude:12.12312313];
[myCar setLongitude:123.12312312313];
NSString *theTextValue = @"Destination";
[myCar setDesp:theTextValue];
以下是我的代码:
Checkpoints.m
#import "CheckPoints.h"
@implementation CheckPoints
@synthesize desp = _desp;
@synthesize latitude = _latitude;
@synthesize longitude = _longitude;
@synthesize state = _state;
- (NSString *) getDesp
{
return desp;
}
- (float *) getLatitude
{
return latitude;
}
- (float *) getLongitude
{
return longitude;
}
- (int *) getState
{
return state;
}
- (void)setDesp:(NSString *)valueDesp {
[self setDesp:valueDesp];
}
- (void)setLatitude:(float )valueLatitude {
// self.latitude = valueLatitude;
[self setLatitude:valueLatitude];
}
- (void)setLongitude:(float )valueLongitude {
[self setLongitude:valueLongitude];
}
- (void)setState:(int )valueState {
[self setState:valueState]; <---this segment
}
@end
Checkpoints.h
#import <GoogleMaps/GoogleMaps.h>
#import <Foundation/Foundation.h>
@interface CheckPoints : NSObject<CLLocationManagerDelegate> {
NSString *desp;
float* latitude;
float* longitude;
int* state;
}
@property (nonatomic, strong) NSString* desp;
@property (nonatomic, assign) float* latitude;
@property (nonatomic, assign) float* longitude;
@property (nonatomic, assign) int* state;
- (NSString *)getDesp;
- (float *)getLatitude;
- (float *)getLongitude;
- (int *)getState;
- (void)setDesp:(NSString *)valueDesp;
- (void)setLatitude:(float )valueLatitude;
- (void)setLongitude:(float )valueLongitude;
- (void)setState:(int )valueState;
@end
答案 0 :(得分:-1)
你在这里做了一个无限循环。在setter中,您应该在实例变量中设置值。您正在从setter方法的主体再次调用setter方法。因此,它将创建一个无限循环。这将超出您的堆栈,您的应用程序将崩溃。请参阅下面的代码,以使您的setter方法正确。
- (void)setState:(int )valueState {
_state = valueState;
}
以相同的方式修改其他setter方法。