Objective-C合计类方法未按预期工作
所以我用Objective-C写了我的第一堂课,我发现了一个我似乎无法解决的奇怪问题。我创建了一个名为Vector的类,并实现了一个名为addUpWith的方法。一切正常,但输出屏幕始终显示以下内容:Objective-C合计类方法未按预期工作,objective-c,Objective C,所以我用Objective-C写了我的第一堂课,我发现了一个我似乎无法解决的奇怪问题。我创建了一个名为Vector的类,并实现了一个名为addUpWith的方法。一切正常,但输出屏幕始终显示以下内容: (6, -4) 而它应该输出 (8, 7) 下面是我使用的三个文件: main.m #import <Foundation/Foundation.h> #import "Vector_Class.h" int main(int argc, const char * argv[])
(6, -4)
而它应该输出
(8, 7)
下面是我使用的三个文件:
main.m
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Vector *vector1 = [Vector initWithX:5 y:9];
//NSLog(vector1.string);
Vector *vector2 = [Vector initWithX:3 y:-2];
//NSLog([vector2 string]);
Vector *sum = [vector1 addUpWith:vector2];
NSLog(@"(%i, %i)", [sum x], [sum y]);
}
return 0;
}
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
@implementation Vector : NSObject
//Variables
int x = 0;
int y = 0;
//Initializer
+ (Vector*) initWithX:(int) x y:(int) y {
Vector *vector = [Vector new];
[vector setX: x];
[vector setY: y];
return vector;
}
//Getters
- (int) x {
return x;
}
- (int) y {
return y;
}
//Setters
-(void) setX: (int) newValue {
x = newValue;
}
-(void) setY: (int) newValue {
y = newValue;
}
- (Vector*) addUpWith: (Vector*) other {
int newX = x + [other x];
int newY = y + [other y];
Vector *vector = [Vector initWithX:newX y:newY];
return vector;
}
- (NSString*) string {
return [NSString stringWithFormat:@"(%i, %i)", x, y];
}
@end
Vector_Class.m
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
Vector *vector1 = [Vector initWithX:5 y:9];
//NSLog(vector1.string);
Vector *vector2 = [Vector initWithX:3 y:-2];
//NSLog([vector2 string]);
Vector *sum = [vector1 addUpWith:vector2];
NSLog(@"(%i, %i)", [sum x], [sum y]);
}
return 0;
}
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
@implementation Vector : NSObject
//Variables
int x = 0;
int y = 0;
//Initializer
+ (Vector*) initWithX:(int) x y:(int) y {
Vector *vector = [Vector new];
[vector setX: x];
[vector setY: y];
return vector;
}
//Getters
- (int) x {
return x;
}
- (int) y {
return y;
}
//Setters
-(void) setX: (int) newValue {
x = newValue;
}
-(void) setY: (int) newValue {
y = newValue;
}
- (Vector*) addUpWith: (Vector*) other {
int newX = x + [other x];
int newY = y + [other y];
Vector *vector = [Vector initWithX:newX y:newY];
return vector;
}
- (NSString*) string {
return [NSString stringWithFormat:@"(%i, %i)", x, y];
}
@end
#导入
#导入“Vector_Class.h”
@实现向量:NSObject
//变数
int x=0;
int y=0;
//初始值设定项
+(向量*)initWithX:(int)xy:(int)y{
矢量*矢量=[矢量新];
[向量集x:x];
[向量setY:y];
返回向量;
}
//吸气剂
-(int)x{
返回x;
}
-(int)y{
返回y;
}
//二传手
-(void)setX:(int)newValue{
x=新值;
}
-(void)setY:(int)newValue{
y=新值;
}
-(向量*)与:(向量*)其他相加{
int newX=x+[其他x];
int newY=y+[其他y];
Vector*Vector=[Vector initWithX:newX y:newY];
返回向量;
}
-(NSString*)字符串{
返回[NSString stringWithFormat:@“(%i,%i)”,x,y];
}
@结束
任何帮助都将不胜感激
另外,我总是乐于接受健康的批评,因此如果你能指出我犯的任何其他错误,我将不胜感激。你的
x
和y
值与你的向量
类中的值有冲突。您声明了x
和y
属性,但随后也不必要地声明了x
和y
方法以及setX:
和setY:
方法。然后在.m文件中不必要地声明全局x
和y
变量(而不是创建实例变量)
所有这些,您所需要的只是.h中的@属性
行
无关,而不是声明字符串
方法,只需覆盖通过NSObject
提供的标准description
方法
考虑到所有这些,您的代码应该更像:
向量h:
@interface Vector : NSObject
//Variables
@property int x;
@property int y;
//Initializer
+ (Vector *)initWithX:(int)x y:(int)y;
//Functions
- (Vector *)addUpWith:(Vector *)other;
@end
@interface Vector : NSObject {
// these are instance variables
int x;
int y;
}
//Initalizer
+ (Vector*) vectorWithX: (int) anX y: (int) aY;
//Getters
- (int) x;
- (int) y;
//Setters
- (void) setX: (int) newValue;
- (void) setY: (int) newValue;
//Functions
- (Vector*)vectorByAddingVector: (Vector*) other;
- (NSString*) string;
@end
Vector.m:
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
@implementation Vector : NSObject
//Initializer
+ (Vector *)initWithX:(int)x y:(int)y {
Vector *vector = [Vector new];
vector.x = x;
vector.y = y;
return vector;
}
- (Vector *)addUpWith:(Vector *)other {
int newX = self.x + other.x;
int newY = self.y + other.y;
Vector *vector = [Vector initWithX:newX y:newY];
return vector;
}
- (NSString *)description {
return [NSString stringWithFormat:@"(%i, %i)", self.x, self.y];
}
@end
@implementation Vector
//Initializer
+ (Vector*) vectorWithX:(int) anX y:(int) aY {
Vector *vector = [Vector new];
[vector setX: anX];
[vector setY: aY];
return vector;
}
//Getters
- (int) x {
return x;
}
- (int) y {
return y;
}
//Setters
-(void) setX: (int) newValue {
x = newValue;
}
-(void) setY: (int) newValue {
y = newValue;
}
- (Vector*) vectorByAddingVector: (Vector*) other {
int newX = x + [other x];
int newY = y + [other y];
Vector *vector = [Vector vectorWithX:newX y:newY];
return vector;
}
- (NSString*) string {
return [NSString stringWithFormat:@"(%i, %i)", x, y];
}
@end
#导入
#导入“Vector_Class.h”
@实现向量:NSObject
//初始值设定项
+(向量*)initWithX:(int)xy:(int)y{
矢量*矢量=[矢量新];
向量x=x;
向量y=y;
返回向量;
}
-(向量*)与:(向量*)其他相加{
int newX=self.x+other.x;
int newY=self.y+other.y;
Vector*Vector=[Vector initWithX:newX y:newY];
返回向量;
}
-(NSString*)说明{
返回[NSString stringWithFormat:@“(%i,%i)”,self.x,self.y];
}
@结束
您与Vector
类中的x
和y
值存在一些冲突。您声明了x
和y
属性,但随后也不必要地声明了x
和y
方法以及setX:
和setY:
方法。然后在.m文件中不必要地声明全局x
和y
变量(而不是创建实例变量)
所有这些,您所需要的只是.h中的@属性
行
无关,而不是声明字符串
方法,只需覆盖通过NSObject
提供的标准description
方法
考虑到所有这些,您的代码应该更像:
向量h:
@interface Vector : NSObject
//Variables
@property int x;
@property int y;
//Initializer
+ (Vector *)initWithX:(int)x y:(int)y;
//Functions
- (Vector *)addUpWith:(Vector *)other;
@end
@interface Vector : NSObject {
// these are instance variables
int x;
int y;
}
//Initalizer
+ (Vector*) vectorWithX: (int) anX y: (int) aY;
//Getters
- (int) x;
- (int) y;
//Setters
- (void) setX: (int) newValue;
- (void) setY: (int) newValue;
//Functions
- (Vector*)vectorByAddingVector: (Vector*) other;
- (NSString*) string;
@end
Vector.m:
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
@implementation Vector : NSObject
//Initializer
+ (Vector *)initWithX:(int)x y:(int)y {
Vector *vector = [Vector new];
vector.x = x;
vector.y = y;
return vector;
}
- (Vector *)addUpWith:(Vector *)other {
int newX = self.x + other.x;
int newY = self.y + other.y;
Vector *vector = [Vector initWithX:newX y:newY];
return vector;
}
- (NSString *)description {
return [NSString stringWithFormat:@"(%i, %i)", self.x, self.y];
}
@end
@implementation Vector
//Initializer
+ (Vector*) vectorWithX:(int) anX y:(int) aY {
Vector *vector = [Vector new];
[vector setX: anX];
[vector setY: aY];
return vector;
}
//Getters
- (int) x {
return x;
}
- (int) y {
return y;
}
//Setters
-(void) setX: (int) newValue {
x = newValue;
}
-(void) setY: (int) newValue {
y = newValue;
}
- (Vector*) vectorByAddingVector: (Vector*) other {
int newX = x + [other x];
int newY = y + [other y];
Vector *vector = [Vector vectorWithX:newX y:newY];
return vector;
}
- (NSString*) string {
return [NSString stringWithFormat:@"(%i, %i)", x, y];
}
@end
#导入
#导入“Vector_Class.h”
@实现向量:NSObject
//初始值设定项
+(向量*)initWithX:(int)xy:(int)y{
矢量*矢量=[矢量新];
向量x=x;
向量y=y;
返回向量;
}
-(向量*)与:(向量*)其他相加{
int newX=self.x+other.x;
int newY=self.y+other.y;
Vector*Vector=[Vector initWithX:newX y:newY];
返回向量;
}
-(NSString*)说明{
返回[NSString stringWithFormat:@“(%i,%i)”,self.x,self.y];
}
@结束
您的问题就在向量的实现中:
@implementation Vector : NSObject
//Variables
int x = 0;
int y = 0;
如果我将代码重写为等效形式,可能会更容易查看:
static int x = 0;
static int y = 0;
@implementation Vector
// other code
@end
通过使用与实例变量相同/相似的名称声明这两个静态(“类”或“共享”)变量,setter和getter最终将设置这些变量,而不是您希望它们设置的实例变量。换句话说,
Vector *vector1 = [Vector initWithX:5 y:9]; // sets x to 5, y to 9
Vector *vector2 = [Vector initWithX:3 y:-2]; // sets x to 3, y to -2
因此,当您使用:
方法调用addup时,您将得到3+3
和-2+-2
,即(6,-4)
我猜你加了
//Variables
int x = 0;
int y = 0;
行以避免编译错误,因为编译器可能说没有要设置的x
和y
变量。这是因为,默认情况下,当您使用@property
声明属性时,合成的实例变量名为\u varName
:例如,在您的示例中为\u x
和\u y
虽然您可以进行许多其他更改,但以下是一个选项。处理Objective-C类中的实例变量、属性和/或getter和setter基本上有两种不同的方法。我在这里展示的方法是在@property
出现之前,像@rmaddy
在他们的答案中所使用的方法一样,更古老和原始的方法。当使用@property
是现代开发的首选时,为什么还要费心显示这种方式呢?您提供的代码似乎是两种不同风格的混合体;一般来说,你会用其中一个。我的示例将向您展示一种方法,因此您可以将其与答案中显示的@property
方法进行对比
向量h:
@interface Vector : NSObject
//Variables
@property int x;
@property int y;
//Initializer
+ (Vector *)initWithX:(int)x y:(int)y;
//Functions
- (Vector *)addUpWith:(Vector *)other;
@end
@interface Vector : NSObject {
// these are instance variables
int x;
int y;
}
//Initalizer
+ (Vector*) vectorWithX: (int) anX y: (int) aY;
//Getters
- (int) x;
- (int) y;
//Setters
- (void) setX: (int) newValue;
- (void) setY: (int) newValue;
//Functions
- (Vector*)vectorByAddingVector: (Vector*) other;
- (NSString*) string;
@end
Vector.m:
#import <Foundation/Foundation.h>
#import "Vector_Class.h"
@implementation Vector : NSObject
//Initializer
+ (Vector *)initWithX:(int)x y:(int)y {
Vector *vector = [Vector new];
vector.x = x;
vector.y = y;
return vector;
}
- (Vector *)addUpWith:(Vector *)other {
int newX = self.x + other.x;
int newY = self.y + other.y;
Vector *vector = [Vector initWithX:newX y:newY];
return vector;
}
- (NSString *)description {
return [NSString stringWithFormat:@"(%i, %i)", self.x, self.y];
}
@end
@implementation Vector
//Initializer
+ (Vector*) vectorWithX:(int) anX y:(int) aY {
Vector *vector = [Vector new];
[vector setX: anX];
[vector setY: aY];
return vector;
}
//Getters
- (int) x {
return x;
}
- (int) y {
return y;
}
//Setters
-(void) setX: (int) newValue {
x = newValue;
}
-(void) setY: (int) newValue {
y = newValue;
}
- (Vector*) vectorByAddingVector: (Vector*) other {
int newX = x + [other x];
int newY = y + [other y];
Vector *vector = [Vector vectorWithX:newX y:newY];
return vector;
}
- (NSString*) string {
return [NSString stringWithFormat:@"(%i, %i)", x, y];
}
@end
您的问题就在Vector
的实现中:
@implementation Vector : NSObject
//Variables
int x = 0;
int y = 0;
如果我将代码重写为等效形式,可能会更容易查看:
static int x = 0;
static int y = 0;
@implementation Vector
// other code
@end
通过使用与实例变量相同/相似的名称声明这两个静态(“类”或“共享”)变量