IOS8 开发之Swift - 自学之路(第一天)

 

What is Swift

“Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.

Swift is a new programming language for iOS and OS X apps that builds on the best of C and Objective-C, without the constraints of C compatibility. Swift adopts safe programming patterns and adds modern features to make programming easier, more flexible, and more fun. Swift’s clean slate, backed by the mature and much-loved Cocoa and Cocoa Touch frameworks, is an opportunity to reimagine how software development works.

Swift has been years in the making. Apple laid the foundation for Swift by advancing our existing compiler, debugger, and framework infrastructure. We simplified memory management with Automatic Reference Counting (ARC). Our framework stack, built on the solid base of Foundation and Cocoa, has been modernized and standardized throughout. Objective-C itself has evolved to support blocks, collection literals, and modules, enabling framework adoption of modern language technologies without disruption. Thanks to this groundwork, we can now introduce a new language for the future of Apple software development.

Swift feels familiar to Objective-C developers. It adopts the readability of Objective-C’s named parameters and the power of Objective-C’s dynamic object model. It provides seamless access to existing Cocoa frameworks and mix-and-match interoperability with Objective-C code. Building from this common ground, Swift introduces many new features and unifies the procedural and object-oriented portions of the language.

Swift is friendly to new programmers. It is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language. It supports playgrounds, an innovative feature that allows programmers to experiment with Swift code and see the results immediately, without the overhead of building and running an app.

Swift combines the best in modern language thinking with wisdom from the wider Apple engineering culture. The compiler is optimized for performance, and the language is optimized for development, without compromising on either. It’s designed to scale from “hello, world” to an entire operating system. All this makes Swift a sound future investment for developers and for Apple.

Swift is a fantastic way to write iOS and OS X apps, and will continue to evolve with new features and capabilities. Our goals for Swift are ambitious. We can’t wait to see what you create with it.”

Reference books&&website

https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11

https://itunes.apple.com/us/course/developing-ios-8-apps-swift/id961180099

https://itunes.apple.com/cn/course/ios-development-in-swift/id950659946

Variable (var) or Constant (let)

What declared with var is considered mutable while let is immutable.

var myVariable = 42
myVariable = 50
let myConstant = 42
____________________________

let implicitInteger = 70
let implicitDouble = 70.0
let explicitDouble: Double = 70
____________________________

let label = "The width is"
let width = 94
let widthLabel = label + String(width)
____________________________

let apples  = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."    //\() is a way to include values in strings
let fruitSummary = "I have \(apples+oranges) pieces of fruit."
________________________

arrys and dictionaries

Create arrays and dictionaries using brackets([]), and access their elements by writing the index or key in brackets.

var shoppingList = ["apple", "banana", "water"]
shoppingList[1] = "a piece of banana"

var occupations = [
    "Jason": "Captain",
    "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"
________________________

let emptyArray= [String]()
let emptyDictionary= [String: Flocat]()
________________________
shopplingList= []
occupations= [:]
________________________
import Foundation
var arrayAny: [AnyObject] = []
arrayAny += [1, "Two", false]
arrayAny.append(4.0)
________________________

Loop

Conditionals: if , switch

loops:for-in, for, while

let individualScore = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScore {
    if(score > 50) {
       teamScore += 3
    } else {
       teamScore += 1
    }
} 
println(teamScore)
teamScore  //This is a simple way to see the value of a variable inside a playgroud.
_____________________________

var optionalString: String? = "Hello"

optionalString ==nil

var optionalName: String? = "Jason Wang"
var greeting = "Hello!"
if let name = optionalName {
    greeting = "Hello, \(name)"
}
_____________________________

var results: [int] = [] //same as var result: Array[Int]
for i: Int in 0..<100 {
    if i % 7 == 0 {
        results.append(i)
    }
}
results
_____________________________
func color(thing: String?) -> String? {
     if let someThing  = thing {
         switch someThing {
             case let x where x.hasPrefix("green"):
                  return "green"
             case "apple":
                  return "red"
             case "pineapple","banana":
                  return "yellow"
             default:
                  return "unknow"
         }
     } else {
         return nil
     }
}
color("apple")
color("green apple")
color("banana")
color("someThing")
color(nil)

Funcation

func location(city: String) -> (Double, Double) {
    var list = [
        "Shanghai" : (12.233, 15.332),
        "Beijing" : (33.222, 31.333)
    ]
    return list[city] ?? (0, 0)
} 
location("Shanghai")
location("Wuhan")

Closure

var odds = [1, 3, 5, 7].map({number in 3*number})
odds

import Foundation
var array: [UInt32] = []
for _ in 0..<10 {
    array.append(arc4random_uniform(UInt32(100)))
}
array
sort(&array){ $0 < $1 }
array

运算符和表达式

运算符

  1. 算数运算符,+,-,*,/,%,++,--
  2. 关系运算发,>,<,>=,<=,!=
  3. 布尔逻辑运算符,!,&&,||
  4. 三元运算符,?:

表达式

  1. 不指定数据类型
  2. 指定数据类型
  3. 可以有分号结束

注释

//

/*   */

数据类型

整型

Swift提供8、16、32、64位形式的有符号及无符号整数,这些整数类型遵循C语言的命名规约,如8位无符号整数的类型为Uint8,32位有符号整数的类型为Int32,与Swift中的所有类型一样,这些整数类型的名称以大写字母开头。

Swift提供了一个整数类型Int:

Swift提供了无符号整数类型UInt:

浮点型

单精度浮点(32位) Float

双精度浮点(64位) Double

数字型与布尔类型

let decimalInteger = 17//表示10进制

let binaryInteger = 0b10001 //二进制17

let octalInteger = 0o21    //八进制17

let hexadecimalInteger = 0x11 //十六进制17

数据类型转换

整型转换

小的转成大的可以,反过来不行。

let one: UInt8 = 1

UInt16(one)

整型与浮点数转换

小的转成大的可以,反过来不行。

let one: UInt8 = 1

Double(one)

杂记

中国Swift开发者的开源项目https://github.com/ipader/SwiftGuide



你可能感兴趣的:(ios,apple,swift)