- •C and Objective-C
- •How this book works
- •How the life of a programmer works
- •Installing Apple’s developer tools
- •Getting started with Xcode
- •Where do I start writing code?
- •How do I run my program?
- •So what is a program?
- •Don’t stop
- •Types
- •A program with variables
- •Challenge
- •Boolean variables
- •When should I use a function?
- •How do I write and use a function?
- •How functions work together
- •Local variables, frames, and the stack
- •Recursion
- •Looking at the frames in the debugger
- •return
- •Global and static variables
- •Challenge
- •printf()
- •Integer operations
- •Integer division
- •Operator shorthand
- •Floating-point numbers
- •Tokens for displaying floating-point numbers
- •The while loop
- •The for loop
- •break
- •continue
- •The do-while loop
- •Challenge
- •Getting addresses
- •Storing addresses in pointers
- •Getting the data at an address
- •How many bytes?
- •NULL
- •Stylish pointer declarations
- •Challenges
- •Writing pass-by-reference functions
- •Avoid dereferencing NULL
- •Creating and using your first object
- •Message anatomy
- •Objects in memory
- •Challenge
- •Nesting message sends
- •Multiple arguments
- •Sending messages to nil
- •Challenge
- •Challenge
- •NSMutableArray
- •Reference pages
- •Quick Help
- •Other options and resources
- •Accessor methods
- •Dot notation
- •Properties
- •self
- •Multiple files
- •Challenge
- •Overriding methods
- •super
- •Challenge
- •Object ownership and ARC
- •Creating the Asset class
- •Adding a to-many relationship to Employee
- •Challenge
- •Retain cycles
- •Weak references
- •Zeroing of weak references
- •For the More Curious: Manual reference counting and ARC History
- •Retain count rules
- •NSArray/NSMutableArray
- •Immutable objects
- •Sorting
- •Filtering
- •NSSet/NSMutableSet
- •NSDictionary/NSMutableDictionary
- •Preprocessor directives
- •#include and #import
- •#define
- •Global variables
- •enum
- •#define vs global variables
- •Writing an NSString to a file
- •Reading files with NSString
- •Writing an NSData object to a file
- •Reading an NSData from a file
- •Target-action
- •Helper objects
- •Notifications
- •Which to use?
- •Callbacks and object ownership
- •Challenge
- •Getting started with iTahDoodle
- •BNRAppDelegate
- •Adding a C helper function
- •Objects in iTahDoodle
- •Model-View-Controller
- •The application delegate
- •Setting up views
- •Running on the iOS simulator
- •Wiring up the table view
- •Adding new tasks
- •Saving task data
- •For the More Curious: What about main()?
- •Edit BNRDocument.h
- •A look at Interface Builder
- •Edit BNRDocument.xib
- •Making connections
- •Revisiting MVC
- •Edit BNRDocument.m
- •Writing init methods
- •A basic init method
- •Using accessors
- •init methods that take arguments
- •Deadly init methods
- •Property attributes
- •Mutability
- •Lifetime specifiers
- •copy
- •More about copying
- •Advice on atomic vs. nonatomic
- •Key-value coding
- •Non-object types
- •Defining blocks
- •Using blocks
- •Declaring a block variable
- •Assigning a block
- •Passing in a block
- •typedef
- •Return values
- •Memory management
- •The block-based future
- •Challenges
- •Anonymous block
- •NSNotificationCenter
- •Bitwise-OR
- •Bitwise-AND
- •Other bitwise operators
- •Exclusive OR
- •Complement
- •Left-shift
- •Right-shift
- •Using enum to define bit masks
- •More bytes
- •Challenge
- •char
- •char *
- •String literals
- •Converting to and from NSString
- •Next Steps
- •Index
#define vs global variables
typedef enum { BlenderSpeedStir = 1, BlenderSpeedChop = 2, BlenderSpeedLiquify = 5, BlenderSpeedPulse = 9, BlenderSpeedIceCrush = 15
} BlenderSpeed;
@interface Blender : NSObject
{
// speed must be one of the five speeds BlenderSpeed speed;
}
// setSpeed: expects one of the five speeds - (void)setSpeed:(BlenderSpeed)x;
@end
Often you won’t care what numbers the five speeds represent – only that they are different from each other. You can leave out the values, and the compiler will make up values for you:
typedef enum { BlenderSpeedStir, BlenderSpeedChop, BlenderSpeedLiquify, BlenderSpeedPulse, BlenderSpeedIceCrush
} BlenderSpeed;
#define vs global variables
Given that you can define a constant using #define or a global variable (which includes the use of enum), why do Objective-C programmers tend to use global variables? In some cases, there are
performance advantages to using global variables. For example, you can use == instead of isEqual: to compare strings if you consistently use the global variable (and an arithmetic operation is faster than a message send). Also, global variables are easier to work with when you are in the debugger.
You should use global variables and enum for constants, not #define.
149
This page intentionally left blank
23
Writing Files with NSString and NSData
The Foundation framework gives the developer a few easy ways to read from and write to files. In this chapter, you’ll try a few of them out.
Writing an NSString to a file
First, let’s see how you would take the contents of an NSString and put it into a file. When you write a string to a file, you need to specify which string encoding you are using. A string encoding describes how each character is stored as an array of bytes. ASCII is a string encoding that defines the letter ‘A’ as being stored as 01000001. In UTF-16, the letter ‘A’ is stored as 0000000001000001.
The Foundation framework supports about 20 different string encodings. UTF can handle an incredible collection of writing systems. It comes in two flavors: UTF-16, which uses two or more bytes for every character, and UTF-8, which uses one byte for the first 128 ASCII characters and two or more for other characters. For most purposes, UTF-8 is a good fit.
Create a new project: a Foundation Command Line Tool called Stringz. In main(), use methods from the NSString class to create a string and write it to the filesystem:
#import <Foundation/Foundation.h> |
|
int main (int argc, const char * argv[]) |
{ |
@autoreleasepool { |
|
NSMutableString *str = [[NSMutableString alloc] init]; for (int i = 0; i < 10; i++) {
[str appendString:@"Aaron is cool!\n"];
}
[str writeToFile:@"/tmp/cool.txt" atomically:YES
encoding:NSUTF8StringEncoding
error:NULL];
NSLog(@"done writing /tmp/cool.txt");
}
return 0;
}
This program will create a text file that you can read and edit in any text editor. The string /tmp/ cool.txt is known as the file path. File paths can be absolute or relative: absolute paths start
151
) ""0 ( %$@--@-6
+ & & ' + " + * & '! " + ! > @ " '' )G & ++ ) ) ) + G * + + + * &' ! 3( & ) ' ) '!4
) ' & + + ) + & ! , ; ' ) ' " + + & + ) ! > ' ; ! > & ' ' &) ! , ) * + ' ' ' ' + ) & + + + " ) & ) & ) !
& ' E6 + ) &) ) ' ) " ) ) ) & ! K ) &) 3 ' 4 & " +' ) " ) ! ( & ' ' & " !
, ' ' *N65 & ! ; ' " 7
@ @
! " #$% &
[ &
2AP A " I ##2AP A$ $,
I-, 9-, JJ% &
# A [' *+ '$,
.
' N65 ; 1 ! # (
. N65 " # 1 1
N65 )
N65 " N66 #
9::-! ) R . ? UT I
"UY56
# UN6H.?B6 5 #
UM S
. ! # 9::-1 # O! " N65 #
! 7
N6-T # I
8 7
N6-T I #U 0T 1 R D #' S
8
.
-,
.
) ) ! + + ' & G ; *['E '! K ) ) & ' !
) &N65) ) GN65@ !
')