ASP源码.NET源码PHP源码JSP源码JAVA源码DELPHI源码PB源码VC源码VB源码Android源码
当前位置:首页 >> 低调看直播体育app软件下载 >> IOS开发 >> Swift语言学习No.1

Swift语言学习No.1

来源:网络整理     时间:2015-01-14     关键词:

本篇文章主要介绍了"Swift语言学习No.1",主要涉及到方面的内容,对于IOS开发感兴趣的同学可以参考一下: 最近开始学习iOS的游戏开发,正好看到一个系列的jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播(https://www.bloc.io/tutorials/swiftris-build-your-fir...

最近开始学习iOS的游戏开发,正好看到一个系列的jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播(https://www.bloc.io/tutorials/swiftris-build-your-first-ios-game-with-swift#!/chapters/675),还用的是Swift,于是就决定开始学习用swift开发。

我这系列blog就依照在swift的学习过程中碰到的问题,然后做一个记录和总计,以备后续复习。

先上程序吧(PS,本文中所有的code都源自开篇所讲到的blog里面)

super.init(size: size)

     anchorPoint = CGPoint(x: 0, y: 1.0)

     let background = SKSpriteNode(imageNamed: "background")
     background.position = CGPoint(x: 0, y: 0)
     background.anchorPoint = CGPoint(x: 0, y: 1.0)
     addChild(background)

1. Let和Var

接触到了swift的第一个关键字 let,万能的stackoverflow上面有个回答讲述的非常全面:

The: let keyword defines a constant:

let theAnswer =42The theAnswer cannot be changed afterwards.The var defines an ordinary variable:What is interesting:

The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once.

Another strange feature:

You can use almost any character you like for constant and variable names, including Unicode characters:

通俗的讲,let定义的是一个常亮,而var定义的是一个变量

同时这个答案更是举了个浅显易懂的例子:

Both var and let are references, therefore let is a const reference. Using fundamental types doesn't really show how let is different than const. The difference comes when using it with class instances (reference types):

classCTest{var str :String=""}let letTest =CTest() letTest.str ="test"// OKletTest.str ="another test"// Still OK//letTest = CTest() // Error

2. anchorPoint

虽然我还没有完全理解透彻anchorPoint的含义,jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播中的这段话简单的介绍了一下:

SpriteKit is based on OpenGL and therefore its coordinate system is opposite to iOS' native cocoa coordinates. 

0, 0 in SpriteKit is the bottom-left corner. 

Swiftris will be drawn from the top down so therefore we anchor our game in the top-left corner of the screen: 0, 1.0

We then create an SKSpriteNode capable of representing our background image and we add it to the scene.

但是从0,0是左下角而0,1是左上角可以得出anchorPoint其实是个百分比。而在苹果的官方文档中指出:

anchorPoint Property

Defines the point in the sprite that corresponds to the node’s position.

Declaration

SWIFT

var anchorPoint: CGPoint

OBJECTIVE-C

@property(nonatomic) CGPoint anchorPoint

Discussion

You specify the value for this property in the unit coordinate space. The default value is (0.5,0.5), which means that the sprite is centered on its position.

Import Statement

import SpriteKit

Availability

Available in iOS 7.0 and later.


这样看来,anchorPoint并不是swift专有的。

3. ?和!

虽然语法看起来很奇怪,不过swift倒也符合人们的直观感觉,这样的代码

var scene: GameScene!
表示GameScence不是一个optional的变量,什么是optional,就是这个变量可以是nil的,也可以不是。

Swift typically enforces instantiation either in-line where you declare the variable or during the initializer, init…. In order to circumvent this requirement we've added an ! after the type.

Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值,也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化。如果在使用变量之前不进行初始化就会报错:

var stringValue : String //error: variable 'stringValue' used before being initialized //let hashValue = stringValue.hashValue // ^ let hashValue = stringValue.hashValue

更多详细的解释直接看这个帖子吧:http://www.ruanman.net/swift/learn/4569.html

以上就介绍了Swift语言学习No.1,包括了方面的内容,希望对IOS开发有兴趣的朋友有所帮助。

本文网址链接:http://www.codes51.com/article/detail_106391.html

相关图片

相关文章