Wednesday, July 16, 2014

Swift - String in swift [ swift programming language apple ios]

Strings 
“Swift strings are represented by the String type, which in turn represents a collection of values of Character type.”
“Unicode-compliant way to work with text in your code. ”
“Swift’s String type is bridged seamlessly to Foundation’s NSString class. ”

“Each Character value represents a single Unicode character.”

“+” to concate string

like format string let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5).


Swift - Some basic about swift / let var in swift [ swift programming language apple ios]

basic
no smicolon(“Semicolons are required, however, if you want to write multiple separate statements on a single line:”
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/in/jEUH0.l), no backet for if , for loop, while loop
let - constant
var for variable
defualt compulsory for switch, no break
All swift code is unicode.
Even u can name variable as unicode character (use option + any key)
type-safe programming language for Cocoa development.
Swift has been in development for 4 years, and was just announced this year at WWDC.
automatic type identification.  The compiler infers it from what value is being set to the variable.

let π = 3.14159
let 你好 = "你好世界"

let 🐶🐮 = "dogcow”

Swift - Basic data types in swift [ swift programming language apple ios]

Integer
UInt8, Int32,Int64,UInt32,UInt64 etc

Float
Double,Float

“Double has a precision of at least 15 decimal digits, whereas the precision of Float can be as little as 6 decimal digits.”

BOOL
Bool
Boolean constant values, true and false:”
Swift’s type safety prevents non-Boolean values from being be substituted for Bool. The following example reports a compile-time error:

let i = 1
if i {
    // this example will not compile, and will report an error

}”

Characters
“Each Character value represents a single Unicode character.”

Swift - Generics (like template in c++) [ swift programming language apple ios]

Generics (like template in c++)
“func repeat<ItemType>(item: ItemType, times: Int) -> ItemType[] {
    var result = ItemType[]()
    for i in 0..times {
        result += item
    }
    return result
}
repeat("knock", 4)”

“// Reimplement the Swift standard library's optional type
enum OptionalValue<T> {
    case None
    case Some(T)
}
var possibleInteger: OptionalValue<Int> = .None
possibleInteger = .Some(100)”


Swift - category in Swift / Extension in swift [ swift programming language apple ios]

Extension like category
“Use extension to add functionality to an existing type, such as new methods and computed properties. ”
“extension Int: ExampleProtocol {
    var simpleDescription: String {
    return "The number \(self)"
    }
    mutating func adjust() {
        self += 42
    }
}
7.simpleDescription”


Swift - Protocol in swift [ swift programming language apple ios]

Protocol
“Classes, enumerations, and structs can all adopt protocols.”
protocol ExampleProtocol {
    var simpleDescription: String { get }
    mutating func adjust()
}
struct SimpleStructure: ExampleProtocol {
    var simpleDescription: String = "A simple structure"
    mutating func adjust() {
        simpleDescription += " (adjusted)"
    }
}

“ mutating keyword in the declaration of SimpleStructure to mark a method that modifies the structure.”

Swift - enum / Enumeration [ swift programming language apple ios]

enum
enum like classes and have methods inside it.
enum Rank: Int {
    case Ace = 1
    case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
    case Jack, Queen, King
    func simpleDescription() -> String {
        switch self {
        case .Ace:
            return "ace"
        case .Jack:
            return "jack"
        case .Queen:
            return "queen"
        case .King:
            return "king"
        default:
            return String(self.toRaw())
        }
    }
}
let ace = Rank.Ace
let aceRawValue = ace.toRaw()



Swift - Classes / Classes in Swift [ swift programming language apple ios]

Classes:-
use class keyword followed by classname

class Shape  {
    var numberOfSides = 0
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}

var shape = Shape()
shape.numberOfSides = 7
var shapeDescription = shape.simpleDescription()

class NamedShape {
    var numberOfSides: Int = 0
    var name: String
    
    init(name: String) {
        self.name = name
    }
    
    func simpleDescription() -> String {
        return "A shape with \(numberOfSides) sides."
    }
}”

“init” and “deinit” like constructor and destructor resp.

class Square: NamedShape    // subclasing
{
var perimeter: Double {//getter and setter
    get {
        return 3.0 * sideLength
    }
    set {
        sideLength = newValue / 3.0
    }
    }

}


“If you don’t need to compute the property but still need to provide code that is run before and after setting a new value, use willSet and didSet. ”
class TriangleAndSquare {
    var triangle: EquilateralTriangle {
    willSet {
        square.sideLength = newValue.sideLength
    }
    }
    var square: Square {
    willSet {
        triangle.sideLength = newValue.sideLength
    }
    }
    init(size: Double, name: String) {
        square = Square(sideLength: size, name: name)
        triangle = EquilateralTriangle(sideLength: size, name: name)
    }
}
var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
triangleAndSquare.square.sideLength
triangleAndSquare.triangle.sideLength
triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
triangleAndSquare.triangle.sideLength


Swift - Function declaration and defination [ swift programming language apple ios]

Function declaration and defination
“Use func to declare a function.”
“func greet(name: String, day: String) -> String {
    return "Hello \(name), today is \(day)."
}”

“Functions can be nested.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/in/jEUH0.l
return multiple values
“func getGasPrices() -> (Double, Double, Double) {
    return (3.59, 3.69, 3.79)
}”

like block
“numbers.map({
    (number: Int) -> Int in
    let result = 3 * number
    return result

    })”

About

Powered by Blogger.