onfirのブログ -26ページ目

onfirのブログ

ブログの説明を入力します。

About This article briefly in WWDC 2014 Apple released the programming language --Swift. Preface Here I think it is necessary to mention Brec Victor's Inventing on Principle, most of the concepts Swift programming environments are derived from Nike Shoes Brec this speech. Next to business. What Swift is? Swift is at WWDC 2014 Apple released the programming language, incorporated herein by reference The Swift Programming Language of the original words: Swift is a new programming language for iOS and 2012 Cheap Nike Air Jordan 1 High Heels For Sale Beige Pink 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 imagine how software development works. Swift is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language simply says:. Swift used to write iOS and OS X programs. (I guess we will not support the other Cock wire system) Swift learned the advantages of C and Objective-C, and more powerful to use. Swift can use existing Cocoa and Cocoa Touch framework. Swift both compiled language performance (Performance) and scripting languages ​​interactivity (Interactive). Swift Language Overview Basic Concepts Note: This section of code derived from The Swift Programming Language in A Swift Tour. Hello, world similar to the scripting language, the following code that is a complete program of Swift. println (\u0026 quot; Hello, world \u0026 quot;) variables and constants Swift use var to declare variables, let declare constants var myVariable = 42myVariable = 50let myConstant = 42 type inference Swift supports type inference (Type Inference), so the above code does not need to specify the type, If you need to specify the type: let explicitDouble: Double = 70Swift not support implicit type conversion (Implicitly casting), so the following code requires explicit type conversion (Explicitly casting): let label = \u0026 quot; The width is \u0026 quot; let width = 94let width = label + String (width) Swift string formatting using \\ (item) ???????????? let apples = 3let oranges = 5let appleSummary = 'I have \\) apples) apples \u0026 quot.; let appleSummary = \u0026 quot;. I have \\ (apples + oranges) pieces of fruit '????? Swift ?? [] ???????? array ????? dictionary ?? var shoppingList = [' catfish ',' water ',' tulips ',' blue paint '] shoppingList [1] =' bottle of water 'var occupations = [' Malcolm ':' Captain ',' Kaylee ':' Mechanic ',] occupations [' Jayne '] =' Public Relations '????????? initializer ????????????? let emptyArray = String [] () let emptyDictionary = Dictionary () ????? ????????? [] ???????? [:]? ??????????? Nike Shoes Global Swift ??????? if switch ?????? ? for-in? for? while? do-while ??? / ????????????? / ???? body ?????? let individualScores = [75, 43, 103 , 87, 12] var teamScore = 0for score in individualScores {if score\u003e 50 {teamScore + = 3} else {teamScore + = 1}} ?????? if? let ?????????? ??? nullable variable ???????????????????????????? var optionalString:? String = 'Hello' optionalString == nilvar optionalName: String? = 'John Appleseed' var gretting = '! Hello' if let name = Air Jordan 11 optionalName {gretting = 'Hello, \\) name) \u0026 quot;} flexible switchSwift The switch supports a variety of comparison operations: let vegetable = \u0026 quot; red pepper \u0026 quot; switch vegetable {case \u0026 quot; celery \u0026 quot ;: let vegetableComment = \u0026 quot; Add some raisins and make ants on a log \u0026 quot;. case \u0026 quot; cucumber \u0026 quot ;, \u0026 quot; watercress \u0026 quot ;: let vegetableComment = \u0026 quot; That would make a good tea sandwich \u0026 quot; case let x where x.hasSuffix (\u0026 quot; pepper \u0026 quot;):. let vegetableComment = \u0026 quot; Is it a spicy \\ (x) 'default: let vegetableComment ='? Everything tastes good in soup '} ??. ??? for-in ???????????????? let interestingNumbers = ['Prime': [2, 3, 5, 7, 11, 13], 'Fibonacci': [1 , 1, 2, 3, 5, 8], 'Square': [1, 4, 9, 16, 25],] var largest = 0for (kind, numbers) in interestingNumbers {for number in numbers {if number\u003e largest {largest = number}}} largestwhile ??? do-while ??? var n = 2while n Swift ????? for ???????????? .. ??????? ?? for-in ???????? var firstForLoop = 0for i in 0..3 {firstForLoop + = i} firstForLoopvar secondForLoop = 0for var i = 0; i ??? Swift ?? .. ??. ..? .. ??????????? ... ????????????????? Swift ?? func ???????? func greet (name: String, day: String) -\u003e String {return 'Hello \\) name), today is \\ (day).'} greet ('Bob', 'Tuesday') ????? Tuple ???? ??? func getGasPrices () -\u003e (Double, Double, Double) {return (3.59, 3.69, 3.79)} getGasPrices () ???????????? func sumOf (numbers: Int ... ) -\u003e Int {var sum = 0 for number in numbers {sum + = number} return sum} sumOf () sumOf (42, 597, 12) ?????????? func returnFifteen () -\u003e Int {var y = 10 func add () {y + = 5} add () return y} returnFifteen () ?????????????????????????? ?? func makeIncrementer () -\u003e (Int -\u003e Int) {func addOne (number: Int) -\u003e Int {return 1 + number} return addOne} var increment = makeIncrementer () increment (7) func hasAnyMatches (list: Int [], condition: Int -\u003e Bool) -\u003e Bool {for item in list {if condition (item) {return true}} return false} func lessThanTen (number: Int) -\u003e Bool {return number ????? ??????????? Swift ????? {} ??????? numbers.map ({(number: Int) -\u003e Int in let result = 3 * number return result}) ?????????????????????? numbers.map ({number in 3 * number}) ???????????????? ?????????????????????????? sort ([1, 5, 3, 12, 2]) {$ 0\u003e $ 1} ????? ????? Swift ?? class ????????????????? class Shape {var numberOfSides = 0 func simpleDescription () -\u003e String {return 'A shape with \\) numberOfSides) sides \u0026 quot;.}} creates an instance of the Shape class and calls its fields and methods. var shape = Shape () shape.numberOfSides = 7var shapeDescription = shape.simpleDescription () by init build an object, you can either use self explicit reference member field (name), it may also be an implicit reference (numberOfSides). class NamedShape {var numberOfSides: Int = 0 var name: String init (name: String) {self.name = name} func simpleDescription () - \u0026 gt; String {return \u0026 quot; A shape with \\ (numberOfSides) sides '}.} ?? deinit ???????????? Swift ???????? override ?????? class Square: NamedShape {var sideLength: Double init (sideLength: Double, name: String) {self.sideLength = sideLength super.init (name: name) numberOfSides = 4} func area () -\u003e Double {return sideLength * sideLength} override func simpleDescription () -\u003e String {return 'A square with sides of length \\) . sideLength) \u0026 quot;}} let test = Square (sideLength: 5.2, name: \u0026 quot; my test square \u0026 quot;) test.area () test.simpleDescription () NOTE: If there's simpleDescription methods are not marked as override, it will caused a compilation error. Properties to simplify the code, Swift was introduced attribute (property), see the following perimeter fields: class EquilateralTriangle: NamedShape {var sideLength: Double = 0.0 init (sideLength: Double, name: String) {self.sideLength = sideLength super.init ( name: name) numberOfSides = 3} var perimeter: Double {get {return 3.0 * sideLength} set {sideLength = newValue / 3.0}} override func simpleDescription () - \u0026 gt; String {return \u0026 quot; An equilateral triagle with sides of length \\ (sideLength) '}} var triangle = EquilateralTriangle (sideLength: 3.1, name:'. a triangle ') triangle.perimetertriangle.perimeter = 9.9triangle.sideLength ??????? setter ????????? ???? newValue? willSet? didSetEquilateralTriangle ????????????????????????????????????????? ? ??????????????????????????????? willSet 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.sideLengthtriangleAndSquare.square = Square (sideLength: 50, name: 'larger square') triangleAndSquare.triangle.sideLength ???? triangle? square ????? sideLength ????? Swift ??????????????????????????????? ????????????????????????? class Counter {var count: Int = 0 func incrementBy (amount: Int, numberOfTimes times: Int) {count + = amount * times}} 2013 Air Jordan 4 (IV) Retro var counter = Counter () counter.incrementBy (2, numberOfTimes: 7) ?? Swift ??????????????????? numberOfTimes ????? times ??????????????????????????????????????????? nil ?????? ?????????????????? nil ???? let optionalSquare:? Square = Square (sideLength: 2.5, name: 'optional square') let sideLength = optionalSquare .sideLength?? ? optionalSquare nil ?? sideLength ?????????????????? enum ???????? Swift ?????????? enum Rank: Int {case Ace = 1 case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten case Jack, Queen, King func simpleDescription () -\u003e 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.Acelet aceRawValue = ace.toRaw () ?? toRaw ? fromRaw ???? raw ?????????????? if let convertedRank = Rank.fromRaw (3) {let threeDescription = convertedRank.simpleDescription ()} ????????? ? member value ??????? actual value ??????? raw value ????????????????????????????? ????????? enum Suit {case Spades, Hearts, Diamonds, Clubs func simpleDescription () -\u003e String {switch self {case .Spades: return 'spades' case .Hearts: return 'hearts' case .Diamonds : return 'diamonds' case .Clubs: return 'clubs'}}} let hearts = Suit.Heartslet heartsDescription = hearts.simpleDescription () ????????????????????? ?????????????????????? enum ServerResponse {case Result (String, String) case Error (String)} let success = ServerResponse.Result ('6:00 am ',' 8:09 pm ') let failure = ServerResponse.Error (' Out of cheese ') switch success {case let .Result Air Max for Men (sunrise, sunset): let serverResponse ='. Sunrise is at \\) sunrise) and sunset is . at \\ (sunset) 'case let .Error (error): let serverResponse =' Failure ... \\) error) \u0026 quot;} structure using the struct keyword to create structure Swift. Structural support features constructors and methods of these classes. The biggest difference is that the structure and class: example of a structure passed by value (passed by value), and the instance of the class passed by reference (passed by reference). struct Card {var rank: Rank var suit: Suit func simpleDescription () - \u0026 gt; String {return \u0026 quot; The \\ (rank.simpleDescription ()) of 180-159220 Nike LeBron 7 VII Soldier White Black Running Shoes Air Jordan 12 \\) suit.simpleDescription ()) \u0026 quot;}} let threeOfSpades = Card ( rank: .Three, suit: .Spades) let threeOfSpadesDescription = threeOfSpades.simpleDescription () protocol (protocol) and expansion (extension) protocol Swift using protocol-defined protocol: protocol ExampleProtocol {var simpleDescription: String {get} mutating func adjust ()} types, enumerations and structures can be realized (adopt) protocol: class SimpleClass: ExampleProtocol {var simpleDescription: String = \u0026 quot; A very simple class \u0026 quot; var anotherProperty:. Int = 69105 func adjust () {simpleDescription + = \u0026 quot; Now . 100% adjusted \u0026 quot;}} var a = SimpleClass () a.adjust () let aDescription = a.simpleDescriptionstruct SimpleStructure: ExampleProtocol {var simpleDescription: String = \u0026 quot; A simple structure \u0026 quot; mutating func adjust () {simpleDescription + = \u0026 quot ; (adjusted) \u0026 quot;}} var b = SimpleStructure () b.adjust () let Air Jordan 1 bDescription = b.simpleDescription expansion extended to add new features (such as a new method or property) on the existing type, Swift use extension Nike shoes online declaring an extension: extension Int: ExampleProtocol {var simpleDescription: String {return \u0026 quot; The number $$ self) \u0026 quot;} mutating func adjust () {self + = 42}} 7.simpleDescription generics (generics) Swift use \u0026 lt; \u0026 gt; to declare a generic function or a generic type: func repeat \u0026 lt; ItemType \u0026 gt; (item: ItemType, times: Int) - \u0026 gt; ItemType [] {var result = ItemType [] () for i in 0..times {result + = item} return result} repeat (\u0026 quot; knock \u0026 quot ;, 4) Swift also supports classes, enumerations, and structures using generics: // Reimplement the Swift standard library's optional typeenum OptionalValue \u0026 lt; T \u0026 gt; {case None case Some ( T)} var possibleInteger: OptionalValue \u0026 lt; Int \u0026 gt; = .NonepossibleInteger = .Some (100) may need to do some generic needs (requirements), such as requirements for a generic type implement an interface or inherit from a particular type, two generic types belong to the same type, etc., Swift described by where these requirements: func anyCommonElements \u0026 lt; T, U where T: Sequence, U: Sequence, T.GeneratorType.Element: Equatable, T.GeneratorType.Element == U.GeneratorType.Element Air Jordan 10 \u0026 gt; (lhs: T, rhs: U) - \u0026 gt; Bool {for lhsItem in lhs {for rhsItem in rhs {if lhsItem == rhsItem {return true}}} return false} anyCommonElements ([1, 2 , 3], [3]) Air Jordan 13 Swift Language Overview on here, friends who are interested please read further The Swift Programming Language. Then we talk about some personal feelings of Swift. Personal experience Note: The following experience is purely personal opinion, for reference only. Although I contacted Swift hodgepodge less than two hours, but it is Lebron James Shoes easy to see that Swift absorb a lot of other programming languages ​​elements, which include, but are not limited to: property (Property), can be a null value (Nullable type) syntax and generic (Generic Type) syntax from C #. Go similar style and format (no semicolon at the end of the sentence, judge the condition does not require brackets). Current examples of Python style reference syntax (using self) and a list of dictionaries declaration syntax. Haskell style range declaration syntax (eg 1..3,1 ... 3). Protocol and extensions from Objective-C (just use their products). Enumerated types like Java (can have members or methods). class and struct concepts and C # is very similar. Note that this does not mean that Swift is plagiarism - actually a programming language can play tricks on those fundamental Moreover Swift election is in my opinion quite a nice feature. Moreover, this hodgepodge there is a benefit - is that any other programming language, developers will not feel very strange Swift - this is very important. Implicit refusal (Refuse implicity) Swift removes some of the implicit operations, such as implicit type conversion and implicit method overloading these two pits, pretty dry. Swift's application direction I think Swift two applications are the following directions: education I mean education programming. The biggest problem is the existing programming language interaction extremely poor, resulting in a steep learning curve. I believe Swift and highly interactive programming environment to break this situation, so that more people - especially young people, to learn programming. Here it is necessary to mention Brec Victor's Inventing on Principle again, I saw this video and you'll understand the strong interaction of the programming environment can bring. Application development of existing iOS and OS X application developers are using the Objective-C, and Objective-C is one and cumbersome (verbose) and the steep learning curve of the language, Air Max for Women if Swift can provide a framework Air Jordans Men's with existing Obj-C Easy interoperability interfaces, I believe there will be a large number of programmers to switch Swift; at the same time, Swift simple syntax will bring a considerable number of other platform developers. In short, the last time a big company launched a high-profile programming language and programming platform, or in 2000 (Microsoft introduced C #), nearly 15 years later, Apple introduced Swift-- As a developer, I am very pleased to be able to witness a the birth of the programming language.Swift Apple's new programming language Introduction