1

I have a c string and need it to break it up into lines (I wont make a NSString of them at this moment). Is there something like an NSMutableArray where I can put this char * in? Or how can I achieve it to make something from the strings what I can access later by index?

Currently I make

char *cline = strtok(data, "\n");
            while(cline)
            {
...
}

Or is it easier todo this when I read the file from disk?

4
  • 1
    Any type you can use in regular C you can use in Objective-C. Commented Mar 13, 2013 at 20:22
  • And you can use NSPointerArray. Or there's an encapsulating object for non-objects whose name I can't recall just now. (Ah, yes -- NSValue.) Commented Mar 13, 2013 at 20:27
  • you can use NSValue but to convert a char back and forth it will create a lot of overhead. Why not just use c pointers with calloc and increase the array when needed yourself? Unfortunately there is no Obj-c container that will store raw c types. Commented Mar 13, 2013 at 20:28
  • @HotLicks did you have an example how NSPointeArray works? Commented Mar 13, 2013 at 20:34

3 Answers 3

2

Use an NSValue to store your char* in an NSMutableArray

Overview

An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids. The purpose of this class is to allow items of such data types to be added to collections such as instances of NSArray and NSSet, which require their elements to be objects. NSValue objects are always immutable.

Sign up to request clarification or add additional context in comments.

5 Comments

No need to create a class; just use NSValue.
ah, maybe thats what i was thinking of. ye NSValue should do the trick
Thank you very much! Does the NSValue frees the pointer when the array get deallocated?
it shouldnt, otherwise stuff will get free'd unintentionally all the time if you just want to delete the NSValue and not the data associated with the pointer
So I should do that in my own deallocate myself? Iterate over my NSValues and free the pointers?
1

You cannot insert a C/C++ pointer into an NSMutableArray, unless it is wrapped in a container like an NSValue or other Objective-C class.

It would be a lot easier, if you want an NSMutableArray, to just convert it to an NSString.

NSArray* strings = [[NSString initWithCString:data encoding:NSUTF8StringEncoding] componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]];

Your other options, if you strictly want to stay in the C/C++ realm would be to have a vector of strings or an array of char*.

Comments

1

As others have already pointed out, to store primitive C types such as a in an Obj-C object such as an instance of NSMutableArray, you would need to wrap them in NSValue objects first.

As an alternative to doing this - if you are wanting to work with pure C strings in Obj-C, don't forget that you can freely mix C with Objective-C source code, so using a normal C array is a perfectly legitimate solution too.

By wrapping the values into an obj-c array you gain the bounds checking and mutability, but if you keep unwrapping the values to work on them as a C string, you might be better sticking with a plain old C string to begin with, to save the overhead.

If you then want to make an NSString, you can simply use the NSString convenience method stringWithFormat:, like so:

char str[50];

// read characters into the buffer from a file...

// When done, convert to an NSString:
NSString *string = [NSString stringWithFormat:@"%s", str];

1 Comment

I jus tested this. It doesn't work. Try NSString *string = [NSString stringWithFormat:@"%@", str];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.