來源:
程式開發與生活玩樂的隨手筆記
http://kkwinds.blogspot.com/2010/04/iphone_19.html
http://kkwinds.blogspot.com/2010/04/iphone.html 
IPHONE SDK DEVELOP & PROGRAMMING TIPS
http://www.flyblog.info/catprogramming/73.html


/*字串連接*/

在java裡面我們常常用到"+"來連接兩個字串 但在objective c似乎不是這樣用
以下用一個例子解釋
float weight=benefit2/7700;
//將float轉成string
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
NSString* Text1=@"你會瘦";
NSString* Text2=@"kg";
//將三個字串連結在一起
NSString *ConText = [NSString stringWithFormat:@"%@%@%@", Text1,Text, Text2];
 

/*型別轉換*/

// 將字串轉換成int
NSString *intString = @"1";
int value = [intString intValue];

// 將字串轉換成float
NSString *floatString = @"10.00";
float value = [floatString floatValue];

// 將字串轉換成double
NSString *doubleString = @"10.000000";
double value = [doubleString doubleValue];

//將float轉換成字串
float weight=benefit2/7700;
NSString* Text = [[NSString alloc] initWithFormat:@"%f", weight];
//釋放記憶體
[Text release];

//將int轉換成字串
int weight=5;
NSString* Text = [[NSString alloc] initWithFormat:@"%d", weight];
//釋放記憶體
[Text release];

/*綜合範例*/ 

-(IBAction)buttonA:(UIButton *)text;{

    NSString *bb = @"You Pressed ";

    aa = @"You Pressed ";

    switch (text.tag) {

        case 1:

            //alert: You Pressed News Feed

            aa = [NSString stringWithFormat:@"%@News Feed",bb];

            break;

        case 2:

            //較有效率 alert: You Pressed Profile

            aa = [aastringByAppendingString:@"Profile"];

            break;

        case 3:

            //alert: You Pressed You Pressed Friends

            aa = [aa stringByAppendingFormat:@"%@Friends",bb];

            break;

        default:

            //int轉字串

            aa = [@"not yet"stringByAppendingString:[[NSStringalloc] initWithFormat:@"(%d)", text.tag]];

            break;

    }

    

    NSLog(@"你按了:%d - %@", text.tag, aa);

    

    //AlertView 提示功能

    UIAlertView * alertA = [[UIAlertViewalloc]initWithTitle:@"News Feed"message:aa

                                                    delegate:self cancelButtonTitle:@"OK"

                                           otherButtonTitles: nil];

    //多加一個按鈕

    [alertA addButtonWithTitle:@"Cancelled"];

    [alertA show];

    //[alertA release];

}


創作者介紹
創作者 我是艾娃小姐 的頭像
艾娃小姐

我是艾娃小姐

艾娃小姐 發表在 痞客邦 留言(0) 人氣( 14123 )