Skip to content

Instantly share code, notes, and snippets.

@pcperini
Created July 10, 2012 01:23
Show Gist options
  • Save pcperini/3080412 to your computer and use it in GitHub Desktop.
Save pcperini/3080412 to your computer and use it in GitHub Desktop.
eero Demo
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
interface FileHelper : Object // "Object" is the same as "NSObject"
property (readonly)
String volumeName // both are readonly properties
String volumeFormat //
+ pathStringWithComponents: Array, return String // class method
openFile: String, [withPermissions: String], return FileHandle // instance method
end
implementation FileHelper // no '@' needed for objc keywords
private // no "{}" needed for instance variables
int iBytesUsed
int iBytesFree
// "components" is the default arg variable name for "pathStringWithComponents"
+ pathStringWithComponents: Array, return String
fullPathName := '' // empty objc string literal implies mutable
for String component in components
fullPathName << '/' + component // string concatenation with '+' and '<<'
return fullPathName
// "withPermissions" was declared optional, so default value is defined here
openFile: String path, withPermissions: String = 'readonly', return FileHandle
FileHandle handle = nil
if permissions == 'readonly' or permissions == 'r'
handle = FileHandle fileHandleForReadingAtPath: path
else if permissions == 'readwrite' or permissions == 'rw'
handle = FileHandle fileHandleForUpdatingAtPath: path
return handle
end
int main()
autoreleasepool
helper := FileHelper new // declare variable "helper" via type inference
files := [] // empty array literal implies mutable
files addObject: (helper openFile: 'readme.txt') // can group message in parens
for FileHandle handle in files // all objects are pointers, so no '*' needed
Log( 'File descriptor is %@', (Number)(handle fileDescriptor) )
handle closeFile
return 0 // semicolons are optional almost everywhere
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
interface ROFileHelper : NSObject // prefixes make for easily-identifiable code
property (readonly)
NSString volumeName // both are readonly properties
NSString volumeFormat //
+ pathStringWithComponents: NSArray, return NSString // class method
- openFile: NSString, [withPermissions: NSString], return NSFileHandle // instance method, long live the "-" prefix!
end
implementation ROFileHelper // no '@' needed for objc keywords
private // no "{}" needed for instance variables
int iBytesUsed
int iBytesFree
// "components" is the default arg variable name for "pathStringWithComponents". but we're all about verbosity, right?
+ pathStringWithComponents: NSArray components, return NSString
fullPathName := '' // empty objc string literal implies mutable
for NSString component in components
fullPathName << '/' + component // string concatenation with '+' and '<<'
return fullPathName
// "withPermissions" was declared optional, so default value is defined here
- openFile: NSString path, withPermissions: NSString = 'readonly', return NSFileHandle
NSFileHandle handle = nil
if permissions == 'readonly' or permissions == 'r'
handle = (NSFileHandle fileHandleForReadingAtPath: path) // parens around calls make it a little easier to see the call.
else if permissions == 'readwrite' or permissions == 'rw'
handle = (NSFileHandle fileHandleForUpdatingAtPath: path)
return handle
end
int main()
autoreleasepool
helper := ROFileHelper new // declare variable "helper" via type inference
files := [] // empty array literal implies mutable
(files addObject: (helper openFile: 'readme.txt')) // can group message in parens
for NSFileHandle handle in files // all objects are pointers, so no '*' needed
fileDescriptorNumber := (NSNumber) (handle fileDescriptor) // if we don't split these two lines up
NSLog( 'File descriptor is %@', fileDescriptorNumber ) //we could end up with parens for a function call, a cast, and a method call *shudder*
handle closeFile
return 0 // semicolons are optional almost everywhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment