码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-02-04 13:04:04      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:ios   swift   apple   

1.1What 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.”

1.2Reference 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

1.3Variable (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."
________________________

1.4arrys 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)
________________________

1.5Loop

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)

1.6Funcation

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")

1.7Closure

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



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

标签:ios   swift   apple   

原文地址:http://blog.csdn.net/jason_wang1989/article/details/43482701

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!