250-ld

Getting strange results, especially when opening another person's project on your own maching? Trying deleting the references and reimporting the files.

246writeToFile – quick file writing

writetofile API Reference

- (BOOL)writeToFile:(NSString *)path 
         atomically:(BOOL)useAuxiliaryFile;

Works with:

NSDictionary
NSArray
NSData
NSString

For more complicated purposes, NSOutputStream might be the best option, but for simply writing a date or even XML this might be the simplest and fasted way.

243Stepping over an Array (or Dictionary)

NSArray *paths = [[NSFileManager defaultManager] 
    directoryContentsAtPath: NSHomeDirectory()];

// either like that...
NSEnumerator *e = [paths objectEnumerator];
for (NSString *p in e) {
    NSLog(@"path: %@", p);
}

// or like that.
for (NSString *p in [paths objectEnumerator]) {
    NSLog(@"path: %@", p);
}

NSDictionary has both [myDict objectEnumerator] and [myDict keyEnumerator].

Fast Enumberation is a feature in Objective-C 2.0, it allows you to step over arrays and dictionaries in a fast, consice, and secure (guarded against mutations) manner.

for (NSString *p in paths) {
    NSLog(@"path: %@", p);
}

// or

NSString *p;
for (p in paths) {
    NSLog(@"path: %@", p);
}

Sometimes things are really as simple as they should be.

240Moving/Exporting SVN’ed Directories

Situation For whatever reasons, you want to move a directory from one svn repository to another.

Problem Once in a repository, every folder gets an invisible .svn folder, where the svn info instored. Deleting them by hand can be tedious. A script might be cumbersome.

Solution Use svn to export the folder. Exporting basically means getting rid of the .svn/ folders. http://svnbook.red-bean.com/en/1.0/re10.html

Usage

svn export folder/ exported-folder/

Update What does svn add --force myDirectory do?

233Modifying Quicktags in WordPress

Open:

/wp-includes/js/quicktags.js

Change/Add/Delete tags to your likening.

220Linking libxml2 in Xcode

How to link libxml2 in Xcode for use with TouchXML?

  1. Copy TouchXML files. They are wrappers to libxml2.
  2. Instead of copying an extra copy of libxml2 to the project frameworks, add the following to the target information:

Header Search Paths:

/usr/include/libxml2
Other Linker Flags:
-lxml2

(Of course assuming, that libxml2 is present at this location on your system.)

In the Target Info, make sure to select the current configuration to see the values. Otherwise you get <Multiple Values> and they look like it's not possible to edit them. Took me a while to figure out, that changing the configuration [top left] was the key. Duh.

218Obj-C, Shortcut: Boolean return value to String

Turning Boolean 1, 0 into a more descriptive description:

NSLog(@"data1 is equal to data2: %@", [data1 isEqualToData:data2] ? @"YES" : @"NO");

214NSMutableArray: setObject vs. setValue

Following situation Using TouchXML to parse XML data, works without problem on example XML files, but crahes on mine. Problem Empty values in my data set. (sometimes <place>somePlace</place>, sometimes <place></place>)

/* CXMLDocument setup & parsing omitted */
NSString *e = [[resultElement childAtIndex:counter] stringValue];
NSString *k = [[resultElement childAtIndex:counter] name];

[blogItem setObject:e forKey:k];
// crashes when e is empty. Displays a (null), is nil

Solution 1 Check for empty e, replace nil with empty string

// check if element is empty
if ( nil == e ) {   // ...or the less elegant (0 == [e length])
    e = @"";
}
[blogItem setObject:e forKey:k];

Solution 2 Use setValue instead of setObject, as setObject crashes and burns when it encounters nil, whereas setValue specifically deals only with strings and handles nil gracefully.

[blogItem setValue:e forKey:k];

212NSString and NSMutableString

Should have been clear, but was not:

NSString *s = @"aString";
s = @"anotherString";
NSLog(@"%@", s);
// anotherString

NSMutableString *m = @"aMutableString";
[m appendString:@"andAnAppendedString"];
NSLog(@"%@", m);
// aMutableStringandAnAppendedString

NString allows for the replacement of its whole content by simply assigning a new value.

With NSMutableString it is possible to add/delete/insert strings at arbitrary places in a string.

206NSXMLParser vs libxml2

NSXMLParser parses XML, reports when each node ends, better for very, very large documents.

libxml2 loads whole xml file into memory.

http://code.google.com/p/touchcode/wiki/TouchXML http://code.google.com/p/kissxml/

iPhone SDK Development by Bill Dudney and Chris Adamson

NSXMLParser initWithContentsOfURL: may block GUI while downloading, can not handle HTTP authentication. initWithData: accumulate data with NSURLConnection's delegate -didReceiveResponse, -didReceiveData, -connectionDidFinishLoading.

TouchXML

  • copy TouchXML files to project
  • in "Targets", select target > Get Info > add /usr/include/libxml2 to Header Search Paths - add libxml2.dylib to "Frameworks"

UPDATE instead of adding libxml2.dylib link to the Framework on the system: Other Linker Flags:

-lxml2

Linking libxml2 in Xcode