Objective-C NSString Reference Types

前端 未结 8 1255
情歌与酒
情歌与酒 2021-01-14 17:35

I have a simple question. If I am declaring NSString (ref type) as shown below:

 NSString *johnsMoney = @\"200\";
    NSString *marysMoney = johnsMoney;

           


        
8条回答
  •  礼貌的吻别
    2021-01-14 18:16

    in simply.

    NSString *johnsMoney = @"200";
    //here johnsMoney is declared as NSString, so it will be string type.
    //any thing given inside @"" will be considered as string.
    //currently, johnsMoney contains 200.
    
    NSString *marysMoney = johnsMoney;
    //here marysMoney is declared as NSString, so it will be string type.
    //johnsMoney is already a string. therefore, marysMoney automatically reads the
    //string in johnsMoney. so currently, marysMoney will also be 200.
    
    NSLog(@"Johns Money %@",johnsMoney);
    NSLog(@"Marys Money %@",marysMoney);
    //both will be printed as 200. right?
    
    johnsMoney = @"100";
    //now johnsMoney reassigned as 100.
    //but still marysMoney is 200.
    //there is nothing written for changing maryMoney from 200.
    
    NSLog(@"Johns Money %@",johnsMoney);
    NSLog(@"Marys Money %@",marysMoney);
    

    so i think you've got it. i don't want to think it in a complicated manner by including the pointers.

    Note:if any one feels that it is rubbish, kindly please avoid my answer. am sorry to post it. i just only vomited the pointer concept. i don't know how much correct is my answer.

提交回复
热议问题