5. C# vs Swift – Arrays
●
Objective C
NSArray *myArray = [[NSArray alloc]
init];
NSArray *myArray = [[NSArray alloc]
initWithObjects:@"one", @"two",
@"three", nil];
●Swift
var myArray = String[]();
var myArray = [“one”, “two”, “three”];
●C#
var myArray = new string[];
var myArray = new []{“one”, “two”,
“three”};
6. C# vs Swift - Functions
●
Objective C
- (string)sayHello:(string)name
{
// do something
}
●C#
st ri ng sayHello(st ri ng name) {
// do something
}
●Swift
func sayHello(name: String) -> String {
// do something
}
10. Further Resources
●
Swift Language website.
●
Swift Programming Language: iBook or PDF
●
Using Swift with Cocoa and Objective C: iBook
or PDF
●
Reddit or Medium communities
●
Swift at WWDC
●
Playground demo at WWDC
Notas del editor
The new language from Apple for iOS and MacOS development
In closed development since 2010
Influences: C#, Ruby, Rust, ML, ObjC
ObjC without the C?
Or is it C#/Java with the Cocoa framework
ObjC is really just C, with a thin layer over the top.
C puts a target on your foot, and then puts your foot against your head
It looks weird with the square brackets notation instead of dot notation in c#/Java
It feels weird because of the messaging-passing system vs the method calling system in c#/Java
And this is how you get Xamarin, a wildly overpriced product that saves c# devs having to learn something new.
Swift sets out to avoid the more notoriously destructive qualities of c
Memory management by default
No pointers
Assignments don't return a value
No fall through in switch statements, so no need for break
Variables are always initialized
Array bounds checked, overflows cause exceptions
No integer overflows
All blocks require braces
This is nothing new, almost all third gen c-like languages have all or most of these restrictions.
Memory management and pointers
So perhaps it is better to say it is Java/C# with some ObjC makeup and Cocoa binds
For much of the core language, Swift is indistinguishable from c#
Playground is a visual, interactive Read-Eval-Print Loop
Allows the developer to view the state of the variables in their code
For loops a visual view displays the changing values of variables over time
Visual view can also display UI elements
http://www.youtube.com/watch?v=w87fOAG8fjk#t=6460
Not the dawn of some golden age of Apple development
Still in beta. Language is still having rough edges buffed off
Objective C won't go away overnight:
Years of existing software
The flipside of familiarity for C#/Java devs is unfamiliarity for ObjC developers
Cocoa designed for Objective C
Xamarin.iOS should lose market share
Requires Mac OS
Requires XCode 6, which is still in beta
And totes can only be acquired by fully paid up members of the iOS or Mac Developer Programs