NSString value not persistent












0















I am declaring class variable of type NSString in my "class.h":



@interface class : NSObject<GADInterstitialDelegate, 
GADBannerViewDelegate, GADRewardBasedVideoAdDelegate,
GADNativeAppInstallAdLoaderDelegate, GADNativeContentAdLoaderDelegate>
{
...
NSString* appId;
}


I the "class.mm" file In function "a" i am assigning a value to the variable:



appId = [[dic objectForKey:@"appid"] stringValue]


The string value at this point is correct.



The problem happen when other function call - function "b".



[GoogleMobileAdsMediationTestSuite presentWithAppID:appId onViewController:rootViewController delegate:nil];


when i try to use this appId - it doesn't contain the assigned, instead I assume it contain a memory address.



How can I keep the value of appId in all future references?










share|improve this question




















  • 2





    can you add some additional code

    – Anbu.Karthik
    Nov 19 '18 at 10:06











  • Checking at this code tell me there is no issue. Search for appId and check if it's value is updated some where else, also show code were you are displaying to value of appId.

    – rptwsthi
    Nov 19 '18 at 10:08











  • Is appId a member variable? If yes, are you calling the functions a and b on the same object?

    – SSB95
    Nov 19 '18 at 10:19











  • @rptwsthi can it be that the app id value is being cleared when dic (dictionary) is being cleared? So when the net function calls and dic is no longer valid the appId value points to unfilled address?

    – Michael A
    Nov 19 '18 at 10:19











  • Please show the whole definition. How is that "class variable" declared?

    – Sulthan
    Nov 19 '18 at 11:57
















0















I am declaring class variable of type NSString in my "class.h":



@interface class : NSObject<GADInterstitialDelegate, 
GADBannerViewDelegate, GADRewardBasedVideoAdDelegate,
GADNativeAppInstallAdLoaderDelegate, GADNativeContentAdLoaderDelegate>
{
...
NSString* appId;
}


I the "class.mm" file In function "a" i am assigning a value to the variable:



appId = [[dic objectForKey:@"appid"] stringValue]


The string value at this point is correct.



The problem happen when other function call - function "b".



[GoogleMobileAdsMediationTestSuite presentWithAppID:appId onViewController:rootViewController delegate:nil];


when i try to use this appId - it doesn't contain the assigned, instead I assume it contain a memory address.



How can I keep the value of appId in all future references?










share|improve this question




















  • 2





    can you add some additional code

    – Anbu.Karthik
    Nov 19 '18 at 10:06











  • Checking at this code tell me there is no issue. Search for appId and check if it's value is updated some where else, also show code were you are displaying to value of appId.

    – rptwsthi
    Nov 19 '18 at 10:08











  • Is appId a member variable? If yes, are you calling the functions a and b on the same object?

    – SSB95
    Nov 19 '18 at 10:19











  • @rptwsthi can it be that the app id value is being cleared when dic (dictionary) is being cleared? So when the net function calls and dic is no longer valid the appId value points to unfilled address?

    – Michael A
    Nov 19 '18 at 10:19











  • Please show the whole definition. How is that "class variable" declared?

    – Sulthan
    Nov 19 '18 at 11:57














0












0








0








I am declaring class variable of type NSString in my "class.h":



@interface class : NSObject<GADInterstitialDelegate, 
GADBannerViewDelegate, GADRewardBasedVideoAdDelegate,
GADNativeAppInstallAdLoaderDelegate, GADNativeContentAdLoaderDelegate>
{
...
NSString* appId;
}


I the "class.mm" file In function "a" i am assigning a value to the variable:



appId = [[dic objectForKey:@"appid"] stringValue]


The string value at this point is correct.



The problem happen when other function call - function "b".



[GoogleMobileAdsMediationTestSuite presentWithAppID:appId onViewController:rootViewController delegate:nil];


when i try to use this appId - it doesn't contain the assigned, instead I assume it contain a memory address.



How can I keep the value of appId in all future references?










share|improve this question
















I am declaring class variable of type NSString in my "class.h":



@interface class : NSObject<GADInterstitialDelegate, 
GADBannerViewDelegate, GADRewardBasedVideoAdDelegate,
GADNativeAppInstallAdLoaderDelegate, GADNativeContentAdLoaderDelegate>
{
...
NSString* appId;
}


I the "class.mm" file In function "a" i am assigning a value to the variable:



appId = [[dic objectForKey:@"appid"] stringValue]


The string value at this point is correct.



The problem happen when other function call - function "b".



[GoogleMobileAdsMediationTestSuite presentWithAppID:appId onViewController:rootViewController delegate:nil];


when i try to use this appId - it doesn't contain the assigned, instead I assume it contain a memory address.



How can I keep the value of appId in all future references?







ios objective-c nsstring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 12:49







Michael A

















asked Nov 19 '18 at 10:02









Michael AMichael A

2,5391347106




2,5391347106








  • 2





    can you add some additional code

    – Anbu.Karthik
    Nov 19 '18 at 10:06











  • Checking at this code tell me there is no issue. Search for appId and check if it's value is updated some where else, also show code were you are displaying to value of appId.

    – rptwsthi
    Nov 19 '18 at 10:08











  • Is appId a member variable? If yes, are you calling the functions a and b on the same object?

    – SSB95
    Nov 19 '18 at 10:19











  • @rptwsthi can it be that the app id value is being cleared when dic (dictionary) is being cleared? So when the net function calls and dic is no longer valid the appId value points to unfilled address?

    – Michael A
    Nov 19 '18 at 10:19











  • Please show the whole definition. How is that "class variable" declared?

    – Sulthan
    Nov 19 '18 at 11:57














  • 2





    can you add some additional code

    – Anbu.Karthik
    Nov 19 '18 at 10:06











  • Checking at this code tell me there is no issue. Search for appId and check if it's value is updated some where else, also show code were you are displaying to value of appId.

    – rptwsthi
    Nov 19 '18 at 10:08











  • Is appId a member variable? If yes, are you calling the functions a and b on the same object?

    – SSB95
    Nov 19 '18 at 10:19











  • @rptwsthi can it be that the app id value is being cleared when dic (dictionary) is being cleared? So when the net function calls and dic is no longer valid the appId value points to unfilled address?

    – Michael A
    Nov 19 '18 at 10:19











  • Please show the whole definition. How is that "class variable" declared?

    – Sulthan
    Nov 19 '18 at 11:57








2




2





can you add some additional code

– Anbu.Karthik
Nov 19 '18 at 10:06





can you add some additional code

– Anbu.Karthik
Nov 19 '18 at 10:06













Checking at this code tell me there is no issue. Search for appId and check if it's value is updated some where else, also show code were you are displaying to value of appId.

– rptwsthi
Nov 19 '18 at 10:08





Checking at this code tell me there is no issue. Search for appId and check if it's value is updated some where else, also show code were you are displaying to value of appId.

– rptwsthi
Nov 19 '18 at 10:08













Is appId a member variable? If yes, are you calling the functions a and b on the same object?

– SSB95
Nov 19 '18 at 10:19





Is appId a member variable? If yes, are you calling the functions a and b on the same object?

– SSB95
Nov 19 '18 at 10:19













@rptwsthi can it be that the app id value is being cleared when dic (dictionary) is being cleared? So when the net function calls and dic is no longer valid the appId value points to unfilled address?

– Michael A
Nov 19 '18 at 10:19





@rptwsthi can it be that the app id value is being cleared when dic (dictionary) is being cleared? So when the net function calls and dic is no longer valid the appId value points to unfilled address?

– Michael A
Nov 19 '18 at 10:19













Please show the whole definition. How is that "class variable" declared?

– Sulthan
Nov 19 '18 at 11:57





Please show the whole definition. How is that "class variable" declared?

– Sulthan
Nov 19 '18 at 11:57












3 Answers
3






active

oldest

votes


















0














You know how, see when you assign value it just keep reference form original object but soon you initialize new object with new alloc or call retain, it's a copy of original. You can keep retain or use



NSString *entityName = [[NSString alloc] initWithString:[[dic objectForKey:@"appid"] stringValue]];` 


Or may be you can define appId as:



@property (strong, nonatomic) NSString *appId; 


in interface and, refer it with self.appId.






share|improve this answer































    0















    So i got it to work by doing this: appId = [[dic objectForKey:@"appid"] retain];




    If that compiles, it means that you've turned off ARC (automatic reference counting) for that file or for your whole project. There's little reason to do that these days, especially if you're not very familiar with the manual reference counting rules. The best solution is almost certainly to turn ARC back on and remove the retain call.




    So that means that the appId my assumption was correct and appId was referencing the "dic" object all this time? The question is how can i detach the appId from the "dic" object so it will stay alive even when the object is cleard?




    Basically, you were assigning a string to appId without retaining the string. When the dictionary you got it from was released, it also released all the objects that it contained, including the string that appId pointed to. With manual reference counting, which you are apparently using, you have to retain any objects that you keep a reference to, and release those objects when you no longer need that reference. If you create an object with alloc/init or new or copy (or some variant of those), you don't need to retain that object, but you do need to release it. You can read more about it in Memory Management Rules.






    share|improve this answer


























    • So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

      – Michael A
      Nov 20 '18 at 15:27











    • Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

      – Caleb
      Nov 20 '18 at 15:59



















    -2














    Try to alloc appID variable like: [NSString string] in "class.m" and then you can change value of this variable.
    You try to assign value to null






    share|improve this answer
























    • You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

      – Caleb
      Nov 19 '18 at 13:51











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372212%2fnsstring-value-not-persistent%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You know how, see when you assign value it just keep reference form original object but soon you initialize new object with new alloc or call retain, it's a copy of original. You can keep retain or use



    NSString *entityName = [[NSString alloc] initWithString:[[dic objectForKey:@"appid"] stringValue]];` 


    Or may be you can define appId as:



    @property (strong, nonatomic) NSString *appId; 


    in interface and, refer it with self.appId.






    share|improve this answer




























      0














      You know how, see when you assign value it just keep reference form original object but soon you initialize new object with new alloc or call retain, it's a copy of original. You can keep retain or use



      NSString *entityName = [[NSString alloc] initWithString:[[dic objectForKey:@"appid"] stringValue]];` 


      Or may be you can define appId as:



      @property (strong, nonatomic) NSString *appId; 


      in interface and, refer it with self.appId.






      share|improve this answer


























        0












        0








        0







        You know how, see when you assign value it just keep reference form original object but soon you initialize new object with new alloc or call retain, it's a copy of original. You can keep retain or use



        NSString *entityName = [[NSString alloc] initWithString:[[dic objectForKey:@"appid"] stringValue]];` 


        Or may be you can define appId as:



        @property (strong, nonatomic) NSString *appId; 


        in interface and, refer it with self.appId.






        share|improve this answer













        You know how, see when you assign value it just keep reference form original object but soon you initialize new object with new alloc or call retain, it's a copy of original. You can keep retain or use



        NSString *entityName = [[NSString alloc] initWithString:[[dic objectForKey:@"appid"] stringValue]];` 


        Or may be you can define appId as:



        @property (strong, nonatomic) NSString *appId; 


        in interface and, refer it with self.appId.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 13:43









        rptwsthirptwsthi

        8,56885392




        8,56885392

























            0















            So i got it to work by doing this: appId = [[dic objectForKey:@"appid"] retain];




            If that compiles, it means that you've turned off ARC (automatic reference counting) for that file or for your whole project. There's little reason to do that these days, especially if you're not very familiar with the manual reference counting rules. The best solution is almost certainly to turn ARC back on and remove the retain call.




            So that means that the appId my assumption was correct and appId was referencing the "dic" object all this time? The question is how can i detach the appId from the "dic" object so it will stay alive even when the object is cleard?




            Basically, you were assigning a string to appId without retaining the string. When the dictionary you got it from was released, it also released all the objects that it contained, including the string that appId pointed to. With manual reference counting, which you are apparently using, you have to retain any objects that you keep a reference to, and release those objects when you no longer need that reference. If you create an object with alloc/init or new or copy (or some variant of those), you don't need to retain that object, but you do need to release it. You can read more about it in Memory Management Rules.






            share|improve this answer


























            • So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

              – Michael A
              Nov 20 '18 at 15:27











            • Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

              – Caleb
              Nov 20 '18 at 15:59
















            0















            So i got it to work by doing this: appId = [[dic objectForKey:@"appid"] retain];




            If that compiles, it means that you've turned off ARC (automatic reference counting) for that file or for your whole project. There's little reason to do that these days, especially if you're not very familiar with the manual reference counting rules. The best solution is almost certainly to turn ARC back on and remove the retain call.




            So that means that the appId my assumption was correct and appId was referencing the "dic" object all this time? The question is how can i detach the appId from the "dic" object so it will stay alive even when the object is cleard?




            Basically, you were assigning a string to appId without retaining the string. When the dictionary you got it from was released, it also released all the objects that it contained, including the string that appId pointed to. With manual reference counting, which you are apparently using, you have to retain any objects that you keep a reference to, and release those objects when you no longer need that reference. If you create an object with alloc/init or new or copy (or some variant of those), you don't need to retain that object, but you do need to release it. You can read more about it in Memory Management Rules.






            share|improve this answer


























            • So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

              – Michael A
              Nov 20 '18 at 15:27











            • Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

              – Caleb
              Nov 20 '18 at 15:59














            0












            0








            0








            So i got it to work by doing this: appId = [[dic objectForKey:@"appid"] retain];




            If that compiles, it means that you've turned off ARC (automatic reference counting) for that file or for your whole project. There's little reason to do that these days, especially if you're not very familiar with the manual reference counting rules. The best solution is almost certainly to turn ARC back on and remove the retain call.




            So that means that the appId my assumption was correct and appId was referencing the "dic" object all this time? The question is how can i detach the appId from the "dic" object so it will stay alive even when the object is cleard?




            Basically, you were assigning a string to appId without retaining the string. When the dictionary you got it from was released, it also released all the objects that it contained, including the string that appId pointed to. With manual reference counting, which you are apparently using, you have to retain any objects that you keep a reference to, and release those objects when you no longer need that reference. If you create an object with alloc/init or new or copy (or some variant of those), you don't need to retain that object, but you do need to release it. You can read more about it in Memory Management Rules.






            share|improve this answer
















            So i got it to work by doing this: appId = [[dic objectForKey:@"appid"] retain];




            If that compiles, it means that you've turned off ARC (automatic reference counting) for that file or for your whole project. There's little reason to do that these days, especially if you're not very familiar with the manual reference counting rules. The best solution is almost certainly to turn ARC back on and remove the retain call.




            So that means that the appId my assumption was correct and appId was referencing the "dic" object all this time? The question is how can i detach the appId from the "dic" object so it will stay alive even when the object is cleard?




            Basically, you were assigning a string to appId without retaining the string. When the dictionary you got it from was released, it also released all the objects that it contained, including the string that appId pointed to. With manual reference counting, which you are apparently using, you have to retain any objects that you keep a reference to, and release those objects when you no longer need that reference. If you create an object with alloc/init or new or copy (or some variant of those), you don't need to retain that object, but you do need to release it. You can read more about it in Memory Management Rules.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 '18 at 6:32









            rptwsthi

            8,56885392




            8,56885392










            answered Nov 19 '18 at 14:03









            CalebCaleb

            109k16152239




            109k16152239













            • So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

              – Michael A
              Nov 20 '18 at 15:27











            • Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

              – Caleb
              Nov 20 '18 at 15:59



















            • So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

              – Michael A
              Nov 20 '18 at 15:27











            • Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

              – Caleb
              Nov 20 '18 at 15:59

















            So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

            – Michael A
            Nov 20 '18 at 15:27





            So in case i alloc / init to the variable i still need to release it, so its quite the same as retain

            – Michael A
            Nov 20 '18 at 15:27













            Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

            – Caleb
            Nov 20 '18 at 15:59





            Yes, when you create an object, you need to release it. If you get an object that someone else created, you need to retain it until you're done with it, and then release it. But again, there's little point in doing that yourself — the compiler can do it better and more reliably than you can, so unless there's some real reason not to, just turn on ARC and let the compiler handle it for you.

            – Caleb
            Nov 20 '18 at 15:59











            -2














            Try to alloc appID variable like: [NSString string] in "class.m" and then you can change value of this variable.
            You try to assign value to null






            share|improve this answer
























            • You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

              – Caleb
              Nov 19 '18 at 13:51
















            -2














            Try to alloc appID variable like: [NSString string] in "class.m" and then you can change value of this variable.
            You try to assign value to null






            share|improve this answer
























            • You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

              – Caleb
              Nov 19 '18 at 13:51














            -2












            -2








            -2







            Try to alloc appID variable like: [NSString string] in "class.m" and then you can change value of this variable.
            You try to assign value to null






            share|improve this answer













            Try to alloc appID variable like: [NSString string] in "class.m" and then you can change value of this variable.
            You try to assign value to null







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 '18 at 11:54









            Nikita NagatkinNikita Nagatkin

            1




            1













            • You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

              – Caleb
              Nov 19 '18 at 13:51



















            • You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

              – Caleb
              Nov 19 '18 at 13:51

















            You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

            – Caleb
            Nov 19 '18 at 13:51





            You can't change the string of the NSString you get back from [NSString string] — you can only replace it with a different string. And if you're just going to replace it, there's no point in allocating it in the first place.

            – Caleb
            Nov 19 '18 at 13:51


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372212%2fnsstring-value-not-persistent%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini