Skip to content

Instantly share code, notes, and snippets.

View macbaszii's full-sized avatar
🏠
Working from home

iMacbaszii macbaszii

🏠
Working from home
View GitHub Profile
@macbaszii
macbaszii / swift.md
Last active August 29, 2015 14:13 — forked from varokas/swift.md

Assignment

let constant = "Constant cannot be changed"
var variable = "Variable can be changed later"

var x,y: Int
let x = 0, y = 0

Types

NSInteger x = 30;
CGFloat y = (CGFloat)x;
let pi: Double = 3.14159
let fpi: Float = 3.14159
let x = Int(pi) // 3
let dpi = Double(pi)
let dgf = CGFloat(pi)
let array = Array("abc") // array = ["a", "b", "c"]
let string = String(["a", "b", "c", "d"]) // string = "abcd"
let intConvert = String(44) // let's try String(37.5)
// AnyObject might be used in this way
// Properties
var destinationViewController: AnyObject
var toolbarItems: [AnyObject]
// Function or Method Arguments
func prepareForSegue(segue: UIStoryboardSegue, sender AnyObject)
func addConstraints(constraints: [AnyObject])
func appendDigit(sender: AnyObject)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if segue.identifier == "ViewImage" {
let imageViewController = segue.destinationViewController as ImgurImageViewController
let cell = sender as ImageCollectionViewCell
if let indexPath = imagesCollectionView.indexPathForCell(cell) {
imageViewController.imgurImage = images[indexPath.row]
}
}
}
@IBAction func touched(sender: AnyObject) {
if let button = sender as? Button {
button.setTitle("Touched", forState: .Normal)
}
}
@IBAction func touched(sender: AnyObject) {
if sender is UISegmentedControl {
let segmentedControl = sender as UISegmentedControl
let selectedIndex = segmentedControl.selectedSegmentIndex
@IBAction func touched(sender: AnyObject) {
if sender is UIButton {
let buttonTitle = (sender as UIButton).currentTitle
}
}
for item in toolbarItems! {
if let barButtonItem = item as? UIBarButtonItem {
// Do something with UIBarButtonItem
}
if let segmentedControl = item as? UISegmentedControl {
// Do something with UISegmentedControl
}
}
//
// PlacePickerViewController.swift
// TaxiReporter
//
// Created by Kiattisak Anoochitarom on 2/27/2558 BE.
// Copyright (c) 2558 Kiattisak Anoochitarom. All rights reserved.
//
import UIKit
import FacebookSDK
package main
import "fmt" // another package contains function
func main() {
var hello = "hello gopher!!" // type inference
// can be shorten in -> hello := "hello gopher!!"
fmt.Println(hello)
arr := [6]int{1, 2, 3, 4, 5}