Looks just like Javascript !

In Javascript, everything is an object : you can call class methods on immediates.
-> "HELLO"

And you can do the same with ObjC strings !

[@"hello" uppercaseString];
-> "HELLO"
I'm using that to create an empty file with writeToFile. Looks quite nice : [@"" writeToFile:logPath atomically:YES encoding:NSUTF8StringEncoding error:nil].

2008 08 28

i think it looks more like Smalltalk than Javascript ;-) now if numbers and nil would also be objects, that'd be nice, but that's not gona happen in Obj-C :-)


Patrick Geiller
2008 08 28

Alas I've never used Smalltalk !

As for numbers and nil … 

// Create an NSNumber
[@6.5 message]; 
// Create an NSNull
[@nil message]; 
// Bools too ?
[@YES message]; 

We could always lobby Apple for that :)

2008 08 28

What's wrong with [[NSFileManager defaultManager] createFileAtPath:logPath contents:[NSData data] attributes:NULL];?

Patrick Geiller
2008 08 28

Nothing ! I used writeToFile because it was the first thing I found in the help.

Qwerty Denzel
2008 08 29

Not a Unicode expert, but I imagine the string @"" with utf-8 encoding would write with a byte order mark at the beginning.

Patrick Geiller
2008 08 29

The file shows up as zero length in Terminal. According to Wikipedia, the BOM is used for UTF8 and UTF32 .

Apparently there is no way to mark the file with a particular encoding … maybe with metadata ?

2008 08 30

Both createFileAtPath:contents:attributes: and writeToFile:atomically:encoding:error: ultimately call open anyway, but you get a lot of additional overhead. Better to do it yourself: man 2 open

Windows likes to use a BOM for UTF-8, but I don't we have that particular problem on OS X.

Patrick Geiller
2008 08 30

You like performance, I prefer convenience ;)

I've dabbled with open/read/write while writing NSLogConsole and quickly came back to ObjC — functions like open are unsearchable, and I've had trouble finding documentation for stuff like ioctl. Even if createFileAtPath is slower, it's much easier to use.

2008 09 03

> You like performance, I prefer convenience ;)

Very true.

> I've dabbled with open/read/write while writing NSLogConsole and quickly came back to ObjC

Keep in mind Objective-C is a superset of C.

> functions like open are unsearchable

Man pages contain example usage, possible pitfalls, existing bugs... Cmd-double click in Xcode to bring up the header file is also extremely useful. (I usually have more trouble with the framework references than with libc)

Patrick Geiller
2008 09 03

Stuff like open and write work just fine, but how do you use stuff like ioctl ? The man page is bare, the header file also, googling it yields differents results for Linux, BSD, … 

The main problem I have with file descriptors is finding documentation on the concept. I know that everything is a file, but using it is practice made me pull my hair. In NSLogConsole, I redirected stderr output to a pipe, this worked just fine when launched from XCode or the Terminal, but blocked when launching from Finder. Seems like because XCode and Terminal open a TTY before launching the app and redirecting stderr to a pipe works fine here, but blocks from Finder.

I tried opening dev/tty, tried to google some more info about pipes, async sockets, (and some exotic options to set with … ioctl), file descriptor concepts and came up with nothing working — I tried the UNIX way and failed, I tried the Cocoa way and succeeded pretty quickly.

Follow me on Twitter
Planet Cocoa

2011 02 22Distance field
2010 07 202Binding through NSApp
2010 05 122Forwarding invocations
2010 02 272Core Image black fringes
2010 02 21Quickest Way to Shell
2010 02 08Who's calling ?
2009 09 2138 ways to use Blocks in Snow Leopard
2009 08 182Bracket Mess
2009 08 124Taming JavascriptCore within and without WebView
2009 04 15Debugging with Activity Monitor
2009 03 25How Core Image Color Tracking works
2009 03 1510Custom NSThemeFrame
2009 03 10Which framework is running ?
2009 03 074CoreUI can paint pretty big
2009 02 18Localization with functions
2009 01 30Did you forget to nest alloc and init?
2009 01 16JSCocoa on the iPhone
2009 01 11Mixing WebView and JavascriptCore
2009 01 09Badge overflow
2009 01 09Find your Garbage Collection leaks with Instruments

Powered by MediaWiki