- Creating Variable
- Switch Case
- Loops
- Nill Coalescing
- Pointers
- Format Specifiers
- NSStrings
- Manipulating and evaluating strings
- NSMutableString
- Primitive Types
- NSNumber
- NSArrays
- NSMutableArray
- Sorting
- Functional Techniques
- NSDictionary
- NSSets
- Generics
- NSValue
- NSData
- NSObject
- NSError
- Blocks
- NSURLSession
- Instance Variables
- Pure Properties
- Private Properties
- Property Attributes
- Categories
Swift
let str = "this is swift"
let i = 42
let arr = ["one", "two"]
print("1.\(arr[0]) 2.\(arr[1])")
Objective-C
NSString *str1 = @"objective-C String!";
int i = 42
NSArray *arr = @[@"arr1", @"arr2"];
if (n == 42)
NSLog(@"1:%@ 2:%@\n", arr[0], arr[1]);
else
NSLog(@"\nThis is %s, %@ \n", str2, str1);
Swift
let n = 42
switch n {
case 1...19:
print("a number between 1...19")
case 20...42:
let x = 10
print("found 42 and x = \(x)")
default:
print("default")
}
Objective-C
int n = 42;
switch (n) {
case 1:
{
int foo = 1;
NSLog(@"found %i", foo);
break;
}
case 42:
NSLog(@"Found %d \n", n);
}
}
Swift
let names = ["str1", "atr2"]
for n in names {
print(n)
}
Objective-C
NSArray *names = @[@"hector", @"Marcela"];
for (NSString *name in names)
NSLog(@"hello %@\n", name);
for(int d = 0; d < 10; d++)
NSLog(@"%d * %d = %d", d, d, d * d);
Swift
let str: String?
str = nil
print("This string is \(str ?? "an empty string")")
Objective-C
NSString *str = nil;
NSLog(@"is nil %@", str ?: @"is nil");
Objective-C
//all objective-c objects must be ponters
//constants
NSString * const first = @"Hello";
NSLog(@"%p, %@", first, first);
NSInteger n = 100;
NSLog(@"%ld %@ \n",n, first);
Objective-C
void learnFormatSpecifiers() {
// %@ means "contents of object"
// %d means "int"
// %p means "print pointer of object"
// %f means "floating point"
// %ld means "long int"
NSLog(@"Euler's number: %0.5f", EULERS_NUMBER);
NSInteger i = 10;
NSLog(@"%ld", (long)i);
Objective-C
NSString *str = @"objective-c String";
NSLog(@"%@", str);
NSInteger number = 42;
NSString *output = [NSString stringWithFormat:@"You Pick %ld", (long)number];
NSLog(@"%@", output);
NSString *output2 = [[NSString alloc] initWithFormat:@"I Picked %ld", (long)number];
NSLog(@"%@\n", output2);
Objective-C
NSString *stringByReaplicingOccurrencesofString = [str stringByReplacingOccurrencesOfString:@"String" withString: @"stringByReaplicingOccurrencesofString"];
NSLog(@"%@ \n", stringByReaplicingOccurrencesofString); // objective-c stringByReaplicingOccurrencesofString
NSString *stringByAppendingString = [output stringByAppendingString:@" - append this string!"];
NSLog(@"%@\n", stringByAppendingString); // You Pick 42 - append this string!
NSString *substringFromIndex = [output substringFromIndex: 4];
NSLog(@"%@\n", substringFromIndex); // Pick 42
NSArray *componentsSeparatedByString = [output componentsSeparatedByString: @" "];
NSLog(@"%ld\n", componentsSeparatedByString.count); // 3
// BOOL checkIsEqualtoString = [@"str" isEqualToString:@"str"];
if ( [@"str" isEqualToString:@"str"]){
NSInteger intToString = [@"42" integerValue];
NSLog(@"%ld\n", (long)intToString * 4);
}
if ([output containsString: @"42"]) {
}
Objective-C
NSMutableString *mutable = [@"mutable String" mutableCopy];
NSMutableString *formatedString = [NSMutableString stringWithFormat:@"%@", @"Object"];
NSLog(@"%@ \n %@ \n", mutable, formatedString);
//store up to 4096 chars
//NSMutableString *longString = [NSMutableString stringWithCapacity: 4096];
NSMutableString *mutateString = [ @"some string" mutableCopy];
[mutateString setString:@"one string"];
NSLog(@"%@\n", mutateString);
Objective-C
//Raw Numbers
int i = 0;
float f = 1.0001;
char c = 'c';
char *str = "C String\0";
Objective-C
NSNumber *i = [NSNumber numberWithInteger:100];
float number = [i floatValue];
NSNumber *luckyNumber = @EULERS_NUMBER;
NSNumber *blueSky = @NO;
NSLog(@"%@\n%@\n", luckyNumber, blueSky);
Objective-C
NSArray *scores = @[@101, @102, @99, @105, @104];
for (NSNumber *score in scores) {
//NSLog(@"%@ \n", score);
long number = [score longValue];
NSLog(@"%ld", number);
}
NSArray *colors = [NSArray arrayWithObjects:@"red", @"green", @"blue", nil];
for (NSString *color in [colors reverseObjectEnumerator]) {
print_l(color); // print funtion i wrote
}
NSLog(@"char at index 0: %@", [colors objectAtIndex:0]);
Objective-C
NSMutableArray *colors = [@[@"red", @"green", @"blue"] mutableCopy];
[colors removeObjectAtIndex:0];
[colors insertObject:@"black" atIndex: 1];
for (NSString *color in [colors reverseObjectEnumerator]) {
print_l(color);
}
[colors removeAllObjects];
- sortedArrayUsingSelector
Objective-C
NSMutableArray *colors = [@[@"red", @"green", @"blue"] mutableCopy];
NSArray *sorted = [colors sortedArrayUsingSelector:@selector(compare:)];
for (NSString *s in sorted) {
print_l(s);
}
Objective-C
NSMutableArray *colors = [@[@"red", @"green", @"blue"] mutableCopy];
// loops through array
[colors enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"%@", obj);
}];
// loop through array and find green
[colors enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if([obj isEqualToString:@"green"]) {
NSLog(@"%@", obj);
*stop = YES;
} else {
NSLog(@"%ld", idx);
}
}];
Objective-C
NSDictionary *students0 = @{@"Hector" : @"iOS6", @"Pedro" : @"iOS100"};
for (NSString *key in students0)
NSLog(@"key: %@ Value: %@ \n", key, students0[key]);
NSMutableDictionary *scores = [[NSMutableDictionary alloc] initWithDictionary: @{}];
[scores addEntriesFromDictionary:@{@"Team76434": @"23232"}];
[scores addEntriesFromDictionary:@{@"Team32134": @"23232"}];
for (NSString *str in scores)
[self print_lStr:scores[str]];
Objective-C
NSSet *mySet = [NSSet setWithObjects:@"one", @"two", nil];
NSSet *yourSet = [NSSet setWithObjects:@"four", @"five", nil];
NSSet *ourSet = [mySet setByAddingObjectsFromSet:yourSet];
NSMutableSet *mutableSet = [ourSet mutableCopy];
[mutableSet addObject:@"six"];
for (NSString *n in ourSet)
[self print_lStr:n];
[mutableSet removeAllObjects];
// NSCountedSet is automaticly mutable and counts number of times objects appear
NSCountedSet *colors = [NSCountedSet setWithArray:@[@"red", @"red", @"green", @"blue"]];
[colors addObjectsFromArray:@[@"red", @"blue", @"orange", @"yellow"]];
for (NSString *str in colors)
NSLog(@"%@ appeares %lu times", str, (unsigned long)[colors countForObject:str]);
Objective-C
NSMutableArray<NSNumber *> *numbers = [NSMutableArray arrayWithCapacity:2];
[numbers addObject:@100];
//[numbers addObject:@"one"]; // generates warning
[numbers addObject:@200];
NSDictionary<NSString *, NSNumber *> *data = @{@"image01": @2032, @"image02": @90232};
Objective-C
// A generic object wrapper that will handle multiple types like CG types
Objective-C
// In Swift its called Data and its identical
Objective-C
// Universal base class, almost all clases inherit from it.
// Both a class and protocol.
NSMutableDictionary *dict = [@{@"may":@"5", @"june": @"6"} mutableCopy];
if ([dict isKindOfClass:[NSDictionary class]]) {
if ([dict respondsToSelector:@selector(removeAllObjects)])
[dict performSelector:@selector(removeAllObjects) withObject:nil afterDelay:0.01];
}
Objective-C
NSError *error;
NSString *fileString = [NSString stringWithContentsOfFile:@"hello.txt" encoding:nil error:&error];
NSLog(@"error: %@", error);
Swift
let print42 = {
print("42");
}
Objective-C
void (^print42)(void) = ^ {
// void - block returns nothing
// (^print42) - set block to a variable called
// (void) - takes no parameters
[self print_lStr:@"42"];
};
print42();
NSNumber* (^get42)(void) = ^{
return @42;
};
Objective-C
- (void)fetchWithCount:(int)count completion:(void (^)(NSDictionary *))completion
{
NSString *countString = [NSString stringWithFormat:@"%d", count];
NSURLComponents *componenets = [NSURLComponents componentsWithURL:_baseURL resolvingAgainstBaseURL:true];
NSURLQueryItem *limit = [NSURLQueryItem queryItemWithName:@"limit" value:countString];
componenets.queryItems = @[limit];
NSURL *url = [componenets URL];
NSURLRequest *request = [NSMutableURLRequest requestWithURL: url];
[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
return;
}
if (!data) {
NSLog(@"Data Error");
return;
}
NSError *jsonError;
NSDictionary *pokemonDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
if (error) {
NSLog(@"error with json: %@:", [error localizedDescription]);
return;
} else {
if (!pokemonDictionary || [pokemonDictionary isKindOfClass:[NSDictionary class]]){
completion(pokemonDictionary);
}
else {
NSLog(@"object is not NSDictionary");
return;
}
}
}] resume];
}
@public
to provide acces after init- ivars ar available
@implementation
Objective-C
@interface HSVNetworking : NSObject {
@public
int limit;
}
NSURL *url = [NSURL URLWithString:@"https://pokeapi.co/api/v2/pokemon/"];
HSVNetworking *networking = [[HSVNetworking alloc] initWithBaseURL:url];
networking->limit = 10;
- A property is a method that gets and sets the value of an instance varaibale.
- Creates an ivar called
_baseURL
. (_ + object name) - You can create your unique ivar with
@synthesize
.
// HSVNetworking.h
@interface HSVNetworking : NSObject
@property NSURL *baseURL;
@end
-
create a class extension
- Create a second
@interface
for the class inside the@implementation
file. - We move private properties to a class extension.
- Create a second
-
NOTE: We can only create class extensions from classes we have source code to. ( canot extend from UIKit classes)
Objective-C
// HSVNetworking.m
// MARK: - Private Properties
@interface HSVNetworking()
- (NSURLRequest *)createPokemonListURLRequest;
@property (nonatomic, copy) NSMutableArray *stringArray;
@end
-
You can choose 1 from each catagory
atomic
,nonatomic
strong
,weak
,copy
,assign
,retain
readwrite
,readonly
-
getter=
andsetter=
attributes allow us to change the name of synthesized getter and setter -
default property attributes:
atomic
,strong
,readwrite
- A Category is similar to an extension in swift.
- Naming convention: ClassName+CategoryName
- Categories can work on any class.
- To create a category, create a new objective-c file, under file type select category and pick a class.
Objective-C
// NSURLRequest+HSVPokemonList.h
@interface NSURLRequest (HSVPokemonList)
+ (NSURLRequest *)HSVPokemonList:(NSURL *)baseURL limit:(int)limit;
@end
// NSURLRequest+HSVPokemonList.m
@implementation NSURLRequest (HSVPokemonList)
+ (NSURLRequest *)HSVPokemonList:(NSURL *)baseURL limit:(int)limit
{
NSString *countString = [NSString stringWithFormat:@"%d", limit];
NSURLComponents *componenets = [NSURLComponents componentsWithURL:baseURL resolvingAgainstBaseURL:true];
NSURLQueryItem *limitQuery = [NSURLQueryItem queryItemWithName:@"limit" value:countString];
componenets.queryItems = @[limitQuery];
NSURL *url = [componenets URL];
NSURLRequest *request = [[self new] initWithURL:url];
return request;
}
@end
Objective-C
@end
Objective-C
@end
Objective-C
@end
Objective-C
@end
Objective-C
@end
Objective-C
@end