mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
Merge branch 'master' of https://github.com/adambard/learnxinyminutes-docs
This commit is contained in:
commit
564f1b20b8
@ -91,6 +91,11 @@ string.
|
|||||||
<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
|
<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
|
||||||
"hello " <> "world" #=> "hello world"
|
"hello " <> "world" #=> "hello world"
|
||||||
|
|
||||||
|
# Ranges are represented as `start..end` (both inclusive)
|
||||||
|
1..10 #=> 1..10
|
||||||
|
lower..upper = 1..10 # Can use pattern matching on ranges as well
|
||||||
|
[lower, upper] #=> [1, 10]
|
||||||
|
|
||||||
## ---------------------------
|
## ---------------------------
|
||||||
## -- Operators
|
## -- Operators
|
||||||
## ---------------------------
|
## ---------------------------
|
||||||
|
@ -55,7 +55,7 @@ int main (int argc, const char * argv[])
|
|||||||
id myObject2 = nil; // Weak typing
|
id myObject2 = nil; // Weak typing
|
||||||
// %@ is an object
|
// %@ is an object
|
||||||
// 'description' is a convention to display the value of the Objects
|
// 'description' is a convention to display the value of the Objects
|
||||||
NSLog(@"%@ and %@", myObject1, [myObject2 description]); // Print "(null) and (null)"
|
NSLog(@"%@ and %@", myObject1, [myObject2 description]); // prints => "(null) and (null)"
|
||||||
|
|
||||||
// String
|
// String
|
||||||
NSString *worldString = @"World";
|
NSString *worldString = @"World";
|
||||||
@ -128,9 +128,10 @@ int main (int argc, const char * argv[])
|
|||||||
// May contain different data types, but must be an Objective-C object
|
// May contain different data types, but must be an Objective-C object
|
||||||
NSArray *anArray = @[@1, @2, @3, @4];
|
NSArray *anArray = @[@1, @2, @3, @4];
|
||||||
NSNumber *thirdNumber = anArray[2];
|
NSNumber *thirdNumber = anArray[2];
|
||||||
NSLog(@"Third number = %@", thirdNumber); // Print "Third number = 3"
|
NSLog(@"Third number = %@", thirdNumber); // prints => "Third number = 3"
|
||||||
// NSMutableArray is mutable version of NSArray allowing to change items in array
|
// NSMutableArray is a mutable version of NSArray, allowing you to change
|
||||||
// and extend or shrink array object. Convenient, but not as efficient as NSArray
|
// the items in the array and to extend or shrink the array object.
|
||||||
|
// Convenient, but not as efficient as NSArray.
|
||||||
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2];
|
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:2];
|
||||||
[mutableArray addObject:@"Hello"];
|
[mutableArray addObject:@"Hello"];
|
||||||
[mutableArray addObject:@"World"];
|
[mutableArray addObject:@"World"];
|
||||||
@ -140,7 +141,7 @@ int main (int argc, const char * argv[])
|
|||||||
// Dictionary object
|
// Dictionary object
|
||||||
NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
|
NSDictionary *aDictionary = @{ @"key1" : @"value1", @"key2" : @"value2" };
|
||||||
NSObject *valueObject = aDictionary[@"A Key"];
|
NSObject *valueObject = aDictionary[@"A Key"];
|
||||||
NSLog(@"Object = %@", valueObject); // Print "Object = (null)"
|
NSLog(@"Object = %@", valueObject); // prints => "Object = (null)"
|
||||||
// NSMutableDictionary also available as a mutable dictionary object
|
// NSMutableDictionary also available as a mutable dictionary object
|
||||||
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2];
|
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithCapacity:2];
|
||||||
[mutableDictionary setObject:@"value1" forKey:@"key1"];
|
[mutableDictionary setObject:@"value1" forKey:@"key1"];
|
||||||
@ -210,7 +211,7 @@ int main (int argc, const char * argv[])
|
|||||||
while (ii < 4)
|
while (ii < 4)
|
||||||
{
|
{
|
||||||
NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value
|
NSLog(@"%d,", ii++); // ii++ increments ii in-place, after using its value
|
||||||
} // => prints "0,"
|
} // prints => "0,"
|
||||||
// "1,"
|
// "1,"
|
||||||
// "2,"
|
// "2,"
|
||||||
// "3,"
|
// "3,"
|
||||||
@ -220,7 +221,7 @@ int main (int argc, const char * argv[])
|
|||||||
for (jj=0; jj < 4; jj++)
|
for (jj=0; jj < 4; jj++)
|
||||||
{
|
{
|
||||||
NSLog(@"%d,", jj);
|
NSLog(@"%d,", jj);
|
||||||
} // => prints "0,"
|
} // prints => "0,"
|
||||||
// "1,"
|
// "1,"
|
||||||
// "2,"
|
// "2,"
|
||||||
// "3,"
|
// "3,"
|
||||||
@ -230,7 +231,7 @@ int main (int argc, const char * argv[])
|
|||||||
for (NSNumber *value in values)
|
for (NSNumber *value in values)
|
||||||
{
|
{
|
||||||
NSLog(@"%@,", value);
|
NSLog(@"%@,", value);
|
||||||
} // => prints "0,"
|
} // prints => "0,"
|
||||||
// "1,"
|
// "1,"
|
||||||
// "2,"
|
// "2,"
|
||||||
// "3,"
|
// "3,"
|
||||||
@ -238,7 +239,7 @@ int main (int argc, const char * argv[])
|
|||||||
// Object for loop statement. Can be used with any Objective-C object type
|
// Object for loop statement. Can be used with any Objective-C object type
|
||||||
for (id item in values) {
|
for (id item in values) {
|
||||||
NSLog(@"%@,", item);
|
NSLog(@"%@,", item);
|
||||||
} // => prints "0,"
|
} // prints => "0,"
|
||||||
// "1,"
|
// "1,"
|
||||||
// "2,"
|
// "2,"
|
||||||
// "3,"
|
// "3,"
|
||||||
@ -255,7 +256,7 @@ int main (int argc, const char * argv[])
|
|||||||
} @finally
|
} @finally
|
||||||
{
|
{
|
||||||
NSLog(@"Finally. Time to clean up.");
|
NSLog(@"Finally. Time to clean up.");
|
||||||
} // => prints "Exception: File Not Found on System"
|
} // prints => "Exception: File Not Found on System"
|
||||||
// "Finally. Time to clean up."
|
// "Finally. Time to clean up."
|
||||||
|
|
||||||
// NSError objects are useful for function arguments to populate on user mistakes.
|
// NSError objects are useful for function arguments to populate on user mistakes.
|
||||||
@ -627,7 +628,7 @@ int main (int argc, const char * argv[]) {
|
|||||||
@end
|
@end
|
||||||
// Instances of Car now have access to the protocol.
|
// Instances of Car now have access to the protocol.
|
||||||
Car *carInstance = [[Car alloc] init];
|
Car *carInstance = [[Car alloc] init];
|
||||||
[[carInstance setEngineOn:NO];
|
[carInstance setEngineOn:NO];
|
||||||
[carInstance turnOnEngine];
|
[carInstance turnOnEngine];
|
||||||
if ([carInstance engineOn]) {
|
if ([carInstance engineOn]) {
|
||||||
NSLog(@"Car engine is on."); // prints => "Car engine is on."
|
NSLog(@"Car engine is on."); // prints => "Car engine is on."
|
||||||
|
Loading…
Reference in New Issue
Block a user