Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to store a value of NSString as char array within a structure in Objective-C?
    primarykey
    data
    text
    <p>I am having trouble assigning the value of an <code>NSString</code> to a <code>char *</code> within a structure of a singleton class. The following is a simplification of my problem. My code utilizes more fields in the structure and passes more <code>NSString</code>s.</p> <p>Say I have a singleton class "SingletonClass" with a structure (I've done my homework with the Apple Documentation and implemented it correctly):</p> <pre><code>struct sampleStruct { char *string1; //other fields } struct1; </code></pre> <p>with this method that sets the value of <code>struct1.string1</code> to the NSString that was passed:</p> <pre><code>- (void)initStructWithString:(NSString *)inputString { //warning: assignment discards qualifiers from pointer target type struct1.string1 = [inputString cStringUsingEncoding:NSUTF8StringEncoding]; } </code></pre> <p>and this method that uses <code>struct1.string1</code>:</p> <pre><code>- (void)useCharArray { //I would obviously be doing something more complex in reality printf("This is the char array: %s", struct1.string1); //doesn't print the value I assigned to it, but garbage } </code></pre> <p>When I call <code>initStructWithString:(NSString *)inputString</code> from one class and try to call <code>useCharArray</code> from another class <code>struct1.string1</code> is a bunch of garbage. During debugging I've confirmed that each class is getting the same instance of this <code>SingletonClass</code>.</p> <p>I'm still learning Objective-C as well as C along the way, so I'm aware it could be a problem with my memory management. I know I <code>[NSString cStringUsingEncoding]</code> should be assigned to a <code>const char *</code> and not a <code>char *</code>, but I don't know how else to go about it since the field in the structure is a <code>char *</code>. I've tried other approaches within <code>initSructWithString</code> such as assigning a new <code>const char *</code> the result of <code>[NSString cStringUsingEncoding]</code> and then copying that with <code>strlcpy</code> to <code>struct1.string1</code>. That approach had the same result.</p> <p>What is the correct technique for storing the value of an <code>NSString</code> in a <code>char *</code> within a struct and how can I ensure that the fields within this struct retain their value as the <code>SingletonClass</code> is used by different classes?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload