I'm parsing some HTML with NSXMLParser and it hits a parser error anytime it encounters an ampersand. I could filter out ampersands before I parse it, but I'd rather parse everything that's there.
It's giving me error 68, NSXMLParserNAMERequiredError: Name is required.
My best guess is that it's a character set issue. I'm a little fuzzy on the world of character sets, so I'm thinking my ignorance is biting me in the ass.
The source HTML uses charset iso-8859-1, so I'm using this code to initialize the Parser:
NSString *dataString = [[[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding] autorelease];
NSData *dataEncoded = [[dataString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES] autorelease];
NSXMLParser *theParser = [[NSXMLParser alloc] initWithData:dataEncoded];
Any ideas?
To the other posters: of course the XML is invalid... it's HTML!
You probably shouldn't be trying to use NSXMLParser for HTML, but rather libxml2
For a closer look at why, check out this article.
Are you sure you have valid XML? You are required to have special characters like & escaped, in the raw XML file you should see &
Encoding the Data through a NSString worked for me, anyway you are autoreleasing an object that was not allocated by yourself (dataUsingEncoding), so it crashes, the solution is :
NSString *dataString = [[NSString alloc] initWithData:data
encoding:NSISOLatin1StringEncoding];
NSData *dataEncoded = [dataString dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:YES];
[dataString release];
NSXMLParser *theParser = [[NSXMLParser alloc] initWithData:dataEncoded];
Related
I'm new to the site and probably even newer to app development, however i'm learning slowly.. lol
So i hit my first big snag, I've researched all over on the web and can't find what I'm looking for, so here we go.
I'm creating an app that calls upon some user inputted files that can and do change on occasion. I've added the UIFileSharing option for this so users can upload files via iTunes.
There are 3 different types of files that will need to be used, a .opt, a .pkg, and a .txt.
Is there some way i can take the files from the directory, read them, and based off the file extension pull them and use them in a UIPickerView wheel? I'm really new at this so please forgive me when i ask you to be specific.
My assumption is to do this in a few steps, first read the files and sort them and place them in an array based on the extension, then use said array to populate the picker, and also to get the count for number of rows etc..
I guess the second two parts are pretty simple to figure out, just setting up a picker to use an array, i just need to know if its possible to build that array based on the user loaded files.
thanks in advance,
Chuck
You'll need to use the UIDocumentInteractionController class.
Simple Example
UIDocumentInteractionController * controller;
NSURL *fileURL=[NSURL fileURLWithPath:[self getFilePath]]
controller = [ UIDocumentInteractionController interactionControllerWithURL: fileURL ];
// How to get File From the Document Directory
-(NSString *)getFilePath{
NSString *documentPath= [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
return [documentPath stringByAppendingPathComponent:#"Your file name"]
}
I solved it guys, thanks for all the help.. Code is below, for anyone interested. Don't forget to define your arrays!
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *bundleDirectory = [fileManager contentsOfDirectoryAtPath:documentsPath error:nil];
NSPredicate *Option = [NSPredicate predicateWithFormat:#"self ENDSWITH '.opt'"];
_OptionArray = [bundleDirectory filteredArrayUsingPredicate:Option];
_OptionPickerData = [_OptionArray copy];
I have been attempting to use NSMutableArray's writeToFile:atomically:
But it has been pointed out to me that this approach is wrong: i.e. iPhone / Objective-C: NSMutableArray writeToFile won't write to file. Always returns NO
It looks like I'm going to have to read the guide on archiving that is referred to in one of the answers in the above link.
Would anyone care to share (or point me towards) some code that helps me accomplish this task?
The method, you've mentioned recursively validates that all the contained objects are property list objects before writing out the file.
Try this:
for (UIImage *image in arrayWithImages) {
NSString *pngPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:#"Documents/%#", #"nameOfTheImage.png"]];
UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];
}
Maybe you can use [UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES]; to write image as jpeg.
Lets say i have the following cstring
char array[1000];
How i can convert it to NSString and vice verse.
Thanks.
Apple's Developer Reference has a good article on this subject. Basically, you will do something like this:
NSString *stringFromUTFString = [[NSString alloc] initWithUTF8String:utf8String];
if the string is UTF8 encoded. Otherwise, you can use initWithCString:encoding: with which you can specify the encoding.
Here is a list of available string encodings.
I have a .plist with 2 key values in it. It is of type Dictionary. I am trying to write value to one of the key values. What's wrong with the code below? I also tried using type "Array". That option also does not work. How can I get it to work using Dictionary & also Array? Anyone has working code example? Thanks. I would appreciate any help.
NSString *filePath = #"myprefs.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:#"test#test.com" forKey:#"Email"];
[plistDict writeToFile:filePath atomically: YES];
You did not initialize the path properly.
If "myprefs.plist" is inside the resources folder of your project then initialize like this:
NSString *filePath = [[NSBundle mainBundle]pathForResource:#"myprefs" ofType:#"plist"];
If its somewhere else in your computer, specify the whole path
I'm working through the Stanford iPhone podcasts and have some basic questions.
The first: why is there no easy string concatenation? (or am I just missing it?)
I needed help with the NSLog below, and have no idea what it's currently doing (the %# part). Do you just substitute those in wherever you need concatenation, and then comma separate the values at the end?
NSString *path = #"~";
NSString *absolutePath = [path stringByExpandingTildeInPath];
NSLog(#"My home folder is at '%#'", absolutePath);
whereas with any other programing language I'd have done it like this:
NSLog(#"My home folder is at " + absolutePath);
Thanks! (Additionally, any good guides/references for someone familiar with Java/C#/etc style syntax transitioning to Objective-C?)
%# is a placeholder in a format string, for a NSString instance.
When you do something like:
NSLog(#"My home folder is at '%#'", absolutePath);
You are telling NSLog to replace the %# placeholder with the string called absolutePath.
Likewise, if you put more placeholders, you can specify more values to replace those placeholders like this:
NSString *absolutePath = #"/home/whatever";
NSLog(#"My home #%d folder is at '%#'", 5, absolutePath);
Will print:
My home #5 is at /home/whatever
An easy way to do string concatenation:
NSString *s1 = #"Hello, ";
NSString *s2 = #"world.";
NSString *s = [NSString stringWithFormat:#"%#%#", s1, s2];
// s will be "Hello, world."
You can't have a + sign as a string concatenate operator, since there is no operator overloading in Objective-C.
Hope it helps.
That is a string format specifier. Basically it allows you to specify a placeholder in the string and the values that are to be inserted into the placeholder's spot. The link I reference above lists the different notations for the placeholders and each placeholder's specific format.
It's just like C#'s String.Format method:
NSLog(String.Format("My home folder is at '{0}'", absolutePath));
You can use NSString +stringWithFormat to do concatenation:
NSString* a = // ...
NSString* b = // ...
NSString* a_concatenated_with_b = [NSString stringWithFormat:#"%#%#",a,b];
The reason for the "%#" is that the string formatting is based off of and extends the printf format strings syntax. These functions take a variable number of arguments, and anything beginning with a percent sign (%) is interpreted as a place holder. The subsequent characters determine the type of the place holder. The standard printf does not use "%#", and since "#" is the symbol commonly used for things that Objective-C adds to the C language, it makes sense that the "#" would symbolize "an Objective-C object".
There is no automatic concatentation using the plus sign (+), because NSString* is a pointer type, and Objective-C is a strict superset of C, and so, consequently, adding to an NSString* object does pointer manipulation. Objective-C does not have any operator overloading feature as in the C++ language.
Also, %# is fairly versatile, as it actually inserts the result of the argument's description method into the result string. For NSString, that's the string's value, other classes can provide useful overrides. Similar to toString in Java, for example.