我是编程新手,目前在我的第一份学校工作中遇到了一些困难。谁能告诉我我在正确的轨道上做事吗?这段代码的目的是添加两个大约 70 位的数字,这在 Objective-C 中使用 int 或 long int 类型无法完成。以下代码不断收到警告:从结果类型“MPInteger *”返回“NSString *__strong”的不兼容指针类型请帮助,我已经研究了很长时间,但一无所获。
MPInteger.h
#import <Foundation/Foundation.h>
@interface MPInteger : NSObject
@property (copy, nonatomic) NSString * description;
-(id) initWithString: (NSString *) x;
-(NSString *) description;
-(MPInteger *) add: (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;
}