私はプログラミングの初心者で、現在、最初の学校の課題でいくつかの問題を抱えています。誰かが私が正しい軌道に乗っていると教えてもらえますか? このコードの目的は、Objective-C の int または long int 型を使用して実行できない 70 桁前後の 2 つの数値を加算することです。次のコードは警告を受け取り続けます:結果の型 '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;
}