2012-01-08 6 views
0

小数点以下の唯一の時刻が小数点が.5であるようにこのプログラムを作成しようとしています。私は[string substringToIndex:[string length] - 2]を使用しようとしていますが、何もしません。それはフロートを追加できないからですか?NSStringの長さを短縮できない

float inchesInField = [sizeField.text floatValue]; 
float shoeSize = inchesInField * 3 - 22; 
NSMutableString *appendedShoeSize = [[NSMutableString alloc] 
              initWithFormat:@"%.1f", shoeSize]; 

if ([appendedShoeSize hasSuffix:@".3"] || [appendedShoeSize hasSuffix:@".5"] || 
    [appendedShoeSize hasSuffix:@".4"] || [appendedShoeSize hasSuffix:@".6"]) 
{ 
    [appendedShoeSize substringToIndex:[appendedShoeSize length] - 2]; 
    [appendedShoeSize appendString:@" ½"]; 
} 

if ([appendedShoeSize hasSuffix:@".0"] || [appendedShoeSize hasSuffix:@".1"] || 
    [appendedShoeSize hasSuffix:@".2"]) 
{ 
    [appendedShoeSize substringToIndex:[appendedShoeSize length] - 2]; 
} 

答えて

3

NSStringのsubstringToIndex:メソッドは新しい文字列を返しますが、元の文字列は変更されないためです。 appendString:大丈夫ですが、substringToIndex:は元の文字列を編集しないようにNSStringのメソッドです。

これはそれを行う必要があります:あなたはsubstringToIndexを呼び出すと

float inchesInField = [sizeField.text floatValue]; 
float shoeSize = inchesInField * 3 - 22; 
NSMutableString *appendedShoeSize = [[NSMutableString alloc] initWithFormat:@"%.1f", shoeSize]; 

if ([appendedShoeSize hasSuffix:@".3"] || [appendedShoeSize hasSuffix:@".5"] || [appendedShoeSize hasSuffix:@".4"] || [appendedShoeSize hasSuffix:@".6"]) { 

    appendedShoeSize = [[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2] mutableCopy]; 
    [appendedShoeSize appendString:@" ½"]; 
} 

if ([appendedShoeSize hasSuffix:@".0"] || [appendedShoeSize hasSuffix:@".1"] || [appendedShoeSize hasSuffix:@".2"]) { 

    appendedShoeSize = [[appendedShoeSize substringToIndex:[appendedShoeSize length] - 2] mutableCopy]; 
} 
+1

もちろん、NSMutableStringで 'deleteCharactersInRange'を使用して、それを適切な位置で切り捨てることができます。 –

+0

これは、NSMutableStringの使用をより確実にする – Daniel

+0

これを試したときに、「NSMutableString * __ strong 'に' NSString * 'から割り当てられた互換性のないポインタ型が返される – AppleGuy

0

:,それは、既存の文字列を変更しません。結果を返します。次のようなものを使用する必要があります。 NSString * result = [appendedShoeSize substringToIndex:[appendedShoeSize length] - 2];

0

Danielが指摘したように、substringToIndex:は新しい文字列を返します。

あなたは、たとえば、replaceCharactersInRange:withString:を使用する必要があります。NSMutableString方法について

NSRange range; range.location = [appendedShoeSize length] - 2; range.length = 2; 
[appendedShoeSize replaceCharactersInRange:range withString:@" ½"] 

以上の基準は、これが答えたが、代替ではありませんhttp://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableString_Class/Reference/Reference.html

0

で見つけることができ、これらすべてのhasSuffix:年代はちょうどすりおろし少し。

float intpart; 
float fracpart = modff(shoeSize, &intpart); 
NSMutableString *appendedShoeSize; 

int size = (int)intpart; 
if (fracpart >= 0.3 && fracpart <= 0.6) 
    appendedShoeSize = [[NSMutableString alloc] initWithFormat:@"%d½", size]; 
else 
{ if (fracpart > 0.6) size += 1; 
    appendedShoeSize = [[NSMutableString alloc] initWithFormat:@"%d", size]; 
} 

modffは、その端数(戻り値)にフロートを分割し、整数(参照引数経由)パーツ:私はこれが何をしたいんだと思います。このコードフラグメントは変更可能な文字列を必要としませんが、後で他のことを行うかもしれないので、その文字列を残しました。このフラグメントは、.7 - > .9を次のサイズにバンプします。