Objective c 不兼容的指针类型?
我是一名编程新手,目前在我的第一次学校工作中遇到了一些困难。有人能告诉我我走对了吗?此代码的目的是在70位左右添加两个数字,这是Objective-C中使用int或long int类型无法完成的。以下代码不断收到警告:不兼容的指针类型从结果类型“MPInteger*”返回“NSString*\uu strong”请帮助,我已经琢磨了很久,但什么也没有得到 MPInteger.hObjective c 不兼容的指针类型?,objective-c,Objective C,我是一名编程新手,目前在我的第一次学校工作中遇到了一些困难。有人能告诉我我走对了吗?此代码的目的是在70位左右添加两个数字,这是Objective-C中使用int或long int类型无法完成的。以下代码不断收到警告:不兼容的指针类型从结果类型“MPInteger*”返回“NSString*\uu strong”请帮助,我已经琢磨了很久,但什么也没有得到 MPInteger.h #import <Foundation/Foundation.h> @interface MPInteg
#import <Foundation/Foundation.h>
@interface MPInteger : NSObject
@property (copy, nonatomic) NSString * description;
-(id) initWithString: (NSString *) x;
-(NSString *) description;
-(MPInteger *) add: (MPInteger *) x;
#导入
@接口MPInteger:NSObject
@属性(副本,非原子)NSString*说明;
-(id)initWithString:(NSString*)x;
-(NSString*)说明;
-(MPInteger*)添加:(MPInteger*)x;
MPInteger.m
#import "MPInteger.h"
@implementation MPInteger
@synthesize description;
-(id) initWithString: (NSString *) x {
self = [super init];
if (self) {
description = [NSString stringWithString: x];
}
return self;
}
-(NSString *) description {
return description;
}
-(MPInteger *) add: (MPInteger *) x
{
int carry = 0;
int index = 0;
int index2 = 0;
int i;
int num1;
int num2;
int result;
index = [description length];
index2 = [x.description length];
while (index2 < index) {
x.description = [x.description stringByPaddingToLength:index withString:@"0" startingAtIndex:0];
}
for (i = index; i <= index || carry != 0; i--) {
num1 = [description characterAtIndex:index];
num2 = [x.description characterAtIndex:index];
result = num1 + num2 + carry;
// if the sum is less than 10
if (result < 10) {
NSString *intString = [NSString stringWithFormat:@"%i", result];
[description replaceValueAtIndex:index inPropertyWithKey:intString withValue:@"%@"];
// replace the description index value with the sum
} else { //otherwise
NSString *intString = [NSString stringWithFormat:@"%i", result];
//getting the index'1' is the value replace the description index value
[description replaceValueAtIndex:index inPropertyWithKey:[intString substringFromIndex:1] withValue:@"%@"];
carry = 1;
}
index--; // decrement index
}
return description;
}
#导入“MPInteger.h”
@实现MPInteger
@综合描述;
-(id)initWithString:(NSString*)x{
self=[super init];
如果(自我){
description=[nsstringwithstring:x];
}
回归自我;
}
-(NSString*)说明{
返回说明;
}
-(MPInteger*)添加:(MPInteger*)x
{
整数进位=0;
int指数=0;
int index2=0;
int i;
int num1;
int num2;
int结果;
索引=[描述长度];
index2=[x.描述长度];
while(index2 对于(i=index;i而言,它几乎就是错误所说的:
@property (copy, nonatomic) NSString * description;
[...]
-(MPInteger *) add: (MPInteger *) x
{
[...]
return description;
}
您说您的add
方法将返回对MPInteger
对象的引用,但您的代码将返回对NSString
的引用。您需要通过为该方法声明返回值的字符串类型或通过返回MPInteger的实例来实现这些匹配。您可以声明m返回MPInteger的方法:
- (MPInteger *)add:(MPInteger *)other;
但最后返回description
,它是一个NSString
实例
您可能打算在返回以下内容之前从字符串中生成一个MPInteger实例:
return [[MPInteger alloc] initWithString:description];
(如果不使用ARC,则添加autorelease
。警告是因为您从add:
方法返回description
,但description
是一个NSString
。您需要description
才能成为MPInteger
不幸的是,NSObject
已经有一个名为
description的方法,该方法返回一个
NSString。每次以格式字符串使用
%@`
NSLog(@"I am %@", self);
将实际调用self
上的description
,并将其放入字符串中以代替%@
在方法结束时,您需要返回一个MPInteger
,而不是description
。请尝试替换
return description;
与
返回新整数。num1=[description characterAtIndex:index];num2=[x.description characterAtIndex:index]
这是有缺陷的逻辑,您必须从两者中减去'0'
,因为characterAtIndex:method会重新返回字符的字符代码,而不是其数值。@H2CO3非常感谢,伙计!但是我如何从字符串的索引中提取数值以执行加法?您不是编写了这个类吗?您似乎没有o已经能够实现add:
方法-您可以从对象中提取字符串并执行(错误的)添加算法,那么您的问题是什么?@H2CO3(i=index;i这三行不能给您这样的警告-您可能误读了编译器消息。如何将“description”转换为MPInteger的实例?@user1637132return[[MPInteger alloc]initWithString:description]
有没有办法将“description”的内容转换成MPInteger?@user1637132请至少记住您编写的initWithString:
方法的用途。
MPInteger newInteger = [MPInteger new];
newInteger.description = description;
return newInteger;