Swift Introduction
Key | Value |
---|---|
Current Swift Version | 4.2 |
Current XCode Version | 9.4 / 10 Beta |
Reading Resources:
-
https://swift.org/
-
https://github.com/iwasrobbed/Swift-CheatSheet
- https://github.com/vsouza/awesome-ios
- https://github.com/matteocrippa/awesome-swift
- https://www.raywenderlich.com/151741/macos-development-beginners-part-1
Development Setup
- Mac Devices [Mac, iOS]
- XCode => https://developer.apple.com/download/more/
- CocoaPods => https://cocoapods.org/
# CocoaPods setup:
brew install cocoapods
brew cask install cocoapods-app
pod setup # this might take a while..
# Create a XCode Project and
# define your project directory:
export PROJECT_DIR=~/Repositories/xcode/Demo
mkdir -p $PROJECT_DIR && cd $PROJECT_DIR
# init CocoaPods for your project by creating an empty file
touch Podfile
cat > Podfile << EOF
# specify the platform + version
# you want to support
platform :osx, '10.10'
# When using Swift, we can do dynamic frameworks:
use_frameworks!
# specify a target:
target 'Demo' do
# Include SwiftyJSON, a nice JSON Library
pod 'SwiftyJSON', '~> 4.1'
end
EOF
# Install Dependencies:
pod install
# Open newly created workspace:
open Demo.xcworkspace
Java → Swift
- Class Definitions
Java | Swift |
---|---|
public MyClass { ... } | class MyClass { ... } |
public Interface MyInterface | protocol MyProtocol { ... } |
public SubClass extends MyClass { ... } | class SubClass : MyClass { ... } |
- Access Modifier
Java 8 | Swift 4 | Visibility |
---|---|---|
open | Classes declared as open can be subclassed outside a module | |
public | public | Within the same Project |
protected | internal (=default) | Within the same Package/Module |
no modifier (=default) | no modifier (=internal) | Within the same Package/Folder |
fileprivate | Within the source file | |
private | private | Within the enclosing declaration |
- Swift: Access Control
Modifier | Class | Subclass | File | Module | World | World++ |
---|---|---|---|---|---|---|
open | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
public | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: |
internal | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: |
fileprivate | :white_check_mark: | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: |
private | :white_check_mark: | :white_check_mark: | :x: | :x: | :x: | :x: |
Note: World++ means that our Classes/Methods can be overridden/extended from outsid our module.