0

私はプログラミングの初心者で、現在、最初の学校の課題でいくつかの問題を抱えています。誰かが私が正しい軌道に乗っていると教えてもらえますか? このコードの目的は、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;
}
4

3 に答える 3

1

これは、エラーの内容とほぼ同じです。

@property (copy, nonatomic) NSString * description;
[...]
-(MPInteger *) add: (MPInteger *) x
{
[...]
    return description;
}

addメソッドはオブジェクトへの参照をMPInteger返すが、コードは代わりにオブジェクトへの参照を返すと言いますNSString。メソッドの戻り値の文字列型を宣言するか、のインスタンスを返すことによって、これらを一致させる必要がありますMPInteger

于 2012-09-01T12:00:04.757 に答える
1

MPIntegerを返すようにメソッドを宣言します。

- (MPInteger *)add:(MPInteger *)other;

しかし、最終的descriptionには、NSStringインスタンスであるを返します。

おそらく、返す前に文字列からMPIntegerインスタンスを作成するつもりでした。

return [[MPInteger alloc] initWithString:description];

autorelease( ARCを使用しない場合は追加してください)。

于 2012-09-01T12:02:32.170 に答える
0

警告は、メソッドdescriptionから返されているためですが、. である必要があります。add:descriptionNSStringdescriptionMPInteger

残念ながら、NSObjectすでにフォーマット文字列にメソッドcalled記述which returns anNSString . It's this method that's called each time you use%@` があります。

NSLog(@"I am %@", self);

は実際に呼び出しdescriptionself、文字列の の代わりに配置し%@ます。

MPIntegerメソッドの最後に、 の代わりにを返す必要がありますdescription。交換してみる

return description;

MPInteger newInteger = [MPInteger new];
newInteger.description = description;
return newInteger;

新しい整数を返します。

于 2012-09-01T12:11:55.430 に答える