I'm creating an iPhone app that needs the location of .wav which is located in the sandbox. I do this with the following:
recordFilePath = (CFStringRef)[NSTemporaryDirectory() stringByAppendingPathComponent: #"recordedFile.wav"];
How can I convert recordFilePath to a string that can then be passed into a C function?
EDIT 1
By the way, this is the statement that is going to receive the c string:
freopen ("locationOfFile/recordedFile.wav","r",stdin);
That will depend on what type your c function expects to get - you can write a c function that accepts NSString* object as parameter. If you want to convert your NSString to char* then you can use one of the following functions:
– cStringUsingEncoding:
– getCString:maxLength:encoding:
– UTF8String
Related
Basically I did this and I got an error:
NSString *searchWord = #"Lilwayne";
NSString *resourceURL = (#"https://api.soundcloud.com/tracks?client_id=546952635e22cc0182d85daceff34381&q=%#&format=json", searchWord);
Error is:
reason: 'Resource 'Lilwayne' is invalid because the scheme is not 'https'.'
I don't understand why this doesn't work. However if I remove the "%#" and replace it with "Lilwayne" it works.
The reason why I am doing it this way is because I have a search feature in my app to search for songs using the soundcloud sdk and I want to dynamically change the value of the variable "searchword" to whatever the user typed in.
Try to use stringWithFormat
NSString *resourceURL = [NSString stringWithFormat:#"https://api.soundcloud.com/tracks?client_id=546952635e22cc0182d85daceff34381&q=%#&format=json", searchWord];
I suggest a trip to the NSString class reference in the Xcode help system. In addition to stringWithFormat, as suggested by Basheer, There is a section on combining strings.
So I have done some searching around so that I could see what it was I was doing with my code, and I couldn't find any answers as to what this very one specific line of code does.
NSString* name = [NSString stringWithUTF8String:countryName];
I know what the rest does (I only had to google how to do this part), it is supposed to take my char* (countryName) and turn it into an NSString so later on I can compare it with the
isEqualToString:
thing. I would just like to know what the following is actually doing to the char, and what does the UTF8String even mean?
I have barely any Objective C programming experience so any feedback is helpful :D
you are not totally right.
this method
Returns a string created by copying the data from a given C array of UTF8-encoded bytes.
so, UTF-8 string here is just a C array of bytes.
Check the documentation here.
It doesn't do anything to the char * string. It's just the input to the method. stringWithUTF8String takes a C-style string (in UTF-8 encoding), and creates an NSString using it as a template.
I am trying to use a library (cfitsio) in an iOS application that heavily depends on using a file pointer to open and maintain access to a data file. I have successfully built an Objective-C Mac app that does what I need to do on iOS. Cocoa touch has methods to load a file in to NSData but I don't see anyway to get a file pointer directly, probably because of the stricter privacy around the file system on iOS. Is there a way to get a file pointer directly or use NSData to make a file pointer for temporary use with the library?
The c function I would be using to open the file is declared below. The file pointer continues to be used in many of the other library functions.
int ffopen(fitsfile **fptr, /* O - FITS file pointer */
const char *name, /* I - full name of file to open */
int mode, /* I - 0 = open readonly; 1 = read/write */
int *status); /* IO - error status */
Try using the NSFileManager which creats a NSFileHandle when you open a file.
This handle has a getter for the fileDescriptor:
Returns the file descriptor associated with the receiver.
- (int)fileDescriptor
Return Value
The POSIX file descriptor associated with the receiver.
see also NSFileHandle
It seems this library declares a type "fitsfile" and uses pointers "fitsfile *".
The function ffopen looks very much like it opens the file and creates the fitsfile* which it returns using a fitsfile**. So you don't have to worry about these files at all, the library does it.
So you'd probably write something like
NSString* path = <Objective-C code to get a path>;
BOOL readOnly = <YES or NO, your choice>
fitsfile* fptr = NULL;
int fstatus = 0;
int fresult = ffopen (&fptr, path.UTF8String, (readonly ? 0 : 1), &fstatus);
Since this library wants the data stored in a file that has a path, the only way to do this is to store the data in a file and pass the path to that file to ffopen.
I am having an iOS Project in which i use some C-Sources.
In the C part I need the path to a file in the mainbundle.
How can I obtain it without using Obj-C?
Basically I want to have the path returned by:
NSLog(#"%s",[[[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"txt"] fileSystemRepresentation]);
So just get the path as you did above, and then get a C string version:
NSString *path = [[[NSBundle mainBundle] pathForResource:#"myfile" ofType:#"txt"] fileSystemRepresentation];
char *cPath = [path cStringUsingEncoding:UTF8StringEncoding];
Just pay attention to the warning in the docs and copy the string if you plan to hang onto it beyond the end of the method/function that you're in:
The returned C string is guaranteed to be valid only until either the
receiver is freed, or until the current autorelease pool is emptied,
whichever occurs first. You should copy the C string or use
getCString:maxLength:encoding: if it needs to store the C string
beyond this time.
Update: If for whatever reason you can't use Foundation, you can do something similar using Core Foundation. You can call CFBundleCopyResourceURL() (or one of its cousins) to get the URL for the resource, and then convert that to a path using CFURLCopyPath().
Best you read the path with objective-c and pass it as char * to the c class by calling a function.
You further could asign a global variable char * which is visible for both objective-c and C.
I am coming to Objective-C from C# without any intermediate knowledge of C. (Yes, yes, I will need to learn C at some point and I fully intend to.) In Apple's Certificate, Key, and Trust Services Programming Guide, there is the following code:
static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey\0";
static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey\0";
I have an NSString that I would like to use as an identifier here and for the life of me I can't figure out how to get that into this data structure. Searching through Google has been fruitless also. I looked at the NSString Class Reference and looked at the UTF8String and getCharacters methods but I couldn't get the product into the structure.
What's the simple, easy trick I'm missing?
Those are C strings: Arrays (not NSArrays, but C arrays) of characters. The last character is a NUL, with the numeric value 0.
“UInt8” is the CoreServices name for an unsigned octet, which (on Mac OS X) is the same as an unsigned char.
static means that the array is specific to this file (if it's in file scope) or persists across function calls (if it's inside a method or function body).
const means just what you'd guess: You cannot change the characters in these arrays.
\0 is a NUL, but including it explicitly in a "" literal as shown in those examples is redundant. A "" literal (without the #) is NUL-terminated anyway.
C doesn't specify an encoding. On Mac OS X, it's generally something ASCII-compatible, usually UTF-8.
To convert an NSString to a C-string, use UTF8String or cStringUsingEncoding:. To have the NSString extract the C string into a buffer, use getCString:maxLength:encoding:.
I think some people are missing the point here. Everyone has explained the two constant arrays that are being set up for the tags, but if you want to use an NSString, you can simply add it to the attribute dictionary as-is. You don't have to convert it to anything. For example:
NSString *publicTag = #"com.apple.sample.publickey";
NSString *privateTag = #"com.apple.sample.privatekey";
The rest of the example stays exactly the same. In this case, there is no need for the C string literals at all.
Obtaining a char* (C string) from an NSString isn't the tricky part. (BTW, I'd also suggest UTF8String, it's much simpler.) The Apple-supplied code works because it's assigning a C string literal to the static const array variables. Assigning the result of a function or method call to a const will probably not work.
I recently answered an SO question about defining a constant in Objective-C, which should help your situation. You may have to compromise by getting rid of the const modifier. If it's declared static, you at least know that nobody outside the compilation unit where it's declared can reference it, so just make sure you don't let a reference to it "escape" such that other code could modify it via a pointer, etc.
However, as #Jason points out, you may not even need to convert it to a char* at all. The sample code creates an NSData object for each of these strings. You could just do something like this within the code (replacing steps 1 and 3):
NSData* publicTag = [#"com.apple.sample.publickey" dataUsingEncoding:NSUnicodeStringEncoding];
NSData* privateTag = [#"com.apple.sample.privatekey" dataUsingEncoding:NSUnicodeStringEncoding];
That sure seems easier to me than dealing with the C arrays if you already have an NSString.
try this
NSString *newString = #"This is a test string.";
char *theString;
theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];