Swift教程NO.2





废话不多说,下面我们就来开始我们的swift之路。

注:若版本不同,内含代可能不适用。

基本运算

“圆周率 3.14”这是小学6年纪数学老师教我们的一条“声明”,它声明了圆周率的值为3.14这个非整数阿拉伯数字。在Swift语言里也可以声明。试试打出以下代码。

var x = 3.14

print(x)

屏幕快照 2017-04-15 下午3.27.16.png

恭喜你,你已经学会了Swift里最基础的代码。其中关键词“var”的意思是变量。什么是变量呢?顾名思义,是可以改变的量,它的对立面是“常量”。在我们声明“x=3.14“时,Swift对此进行判断,并将“x”声明为Int的变量。

Int是什么呢?Int是Integer的缩写,是数值的一种类型,它的意思是没有小数的整数。

而第二行代码中的print的意思是打印,也就是让x的值显示在下方。如果不写这一行代码,下方的窗口就会一片空白。

那么既然变量是可以更改的,我们要如何改变它呢?试试看写出下面的代码。

var x = 3.1415926

print(x)

屏幕快照 2017-04-15 03.35.39 PM.png

很简单,重新给x赋值就行了。

再想一想小学时,老师还教了哪些重要内容?没错,就是加减乘除,可以说是数学很基本的一部分内容。Swift也可以使用加减乘除的简单运算,试试写出下面的代码:

var y = 1+1

Print (y)

屏幕快照 2017-04-15 03.41.03 PM.png

加(+)减(-)乘(*)除(/)四项运算Swift都是支持的。

常量VS

了解了变量,那么前面所说的常量是什么?

常量与变量不同,它是始终不变的,也无法像上面一样去随意更改值。这么说来,变量明显比常量更好用,我们为什么不全部使用变量呢?常量也有它的好处,它比变量更加节约资源。计算机认定常量为永久不会改变的量,所以不必考虑它可能变化的情况。在写代码的时候,将一些不必要的变量改成常量可以有效提高机器性能,这在以后编写程序的时候至关重要。

可以用let来定义一个常量。

屏幕快照 2017-04-15 03.45.55 PM.png

回头看一看所有的图片,就会发现右侧还有一个窗口。它里面也显示了一些东西。比如定义x=1时,它显示1。让它print (y)的时候,显示了”2\n”。它能够帮助你实时了解你写下的代码。

另外解释一下\n是什么意思,它代表着换行。在Swift中,每一次print都是自带换行的,如果不需要换行,可以使用:

print(x, terminator: “”)

如果想要检测的话,可以再print一个别的东西,就会发现它接在了x后面。

除了常量和变量以外,还有别的属性,也就是说其他类型。一个东西的类型平时不修改,就像一个人的性别是很难改变的。只有使用特定的代码才能改。

下面简单介绍几个比较常用的类型。

Bool: 表示是或否。

Int:整数。

Float/Double:小数。

Character:被双引号括起的单个字符。(一定单个,可以打入任何东西,如中文,日文,罗马数字)

String:被双引号括起的多个字符。

上限下限的查询

不过就像人有下限一样,上面介绍的各种类型也是有大小限制的。当然“百度一下我就知道”也可以解决这个问题,然而太麻烦了。直接在Xcode里面查看一下就好了。现在试试写出以下代码:

Int.max

Int.min

【注意】类型的第一个字母都要大写!

屏幕快照 2017-04-15 05.07.45 PM.png

于是那一瞬间就可以看到右边显示出了数字!

在类型名称后面加入.min或.max,可以查看该类型的上限和下限,妈妈再也不用担心我不知道类型的上限和下限了。

 

那么这一节内容就要结束了,大家之后可以在设备上多多尝试哦!这一次提出的其他类型将会在下期呈现!【提前试着玩玩也不错】

我们下期再见。

 

《Swift教程NO.2》上有150条评论

  1. I have noticed you don’t monetize your site, don’t waste your
    traffic, you can earn extra cash every month. You
    can use the best adsense alternative for any type
    of website (they approve all websites), for more details simply
    search in gooogle: boorfe’s tips monetize your website

  2. With havin so much content do you ever run into any issues of plagorism or copyright infringement?
    My blog has a lot of exclusive content I’ve either written myself or
    outsourced but it seems a lot of it is popping it up all over the
    web without my permission. Do you know any techniques to help stop content from being ripped off?
    I’d truly appreciate it.

  3. Nice post. I was checking constantly this blog
    and I am impressed! Very helpful information specifically the last part 🙂 I care for such information a
    lot. I was seeking this certain information for a long time.

    Thank you and good luck.

  4. Hello. I see that you don’t update your page too often. I know that
    writing content is time consuming and boring. But did you
    know that there is a tool that allows you to create new posts using existing content (from article directories or other
    blogs from your niche)? And it does it very well. The new articles are high quality and pass the copyscape test.

    Search in google and try: miftolo’s tools

  5. Hello there, I discovered your website by way
    of Google at the same time as searching for a
    comparable matter, your site came up, it looks good.
    I’ve bookmarked it in my google bookmarks.
    Hello there, simply become alert to your weblog thru Google, and found that it’s really informative.
    I am going to be careful for brussels. I’ll appreciate when you proceed this
    in future. Numerous folks can be benefited out of your writing.
    Cheers!

  6. Hello. I have checked your hourofcode.cn and i see you’ve got some
    duplicate content so probably it is the reason that
    you don’t rank high in google. But you can fix this issue fast.
    There is a tool that creates articles like human, just search in google: miftolo’s tools

  7. cheapest generic viagra generic viagra india http://2xlt.com/space-uid-442500.html
    viagra online online viagra https://www.avchi.cc/home.php?mod=space&uid=422870&do=profile&from=space
    generic viagra for sale does generic viagra work https://crew.ymanage.net/?option=com_k2&view=itemlist&task=user&id=2490408
    generic viagra review generic viagra for sale http://567tm.com/bbs/home.php?mod=space&uid=1453163&do=profile&from=space
    generic viagra 100mg cheapest generic viagra http://www.hachmuth.com/forums/profile/shantaemer
    generic viagra reviews generic viagra http://www.wwegames.net/profile/melvinllama
    viagra generic name viagra online prescription http://newsoft7.ru/engine/redirect.php?url=http://cheapnowaaaa.com/
    can you buy viagra online viagra online canadian pharmacy http://shanghai.bbs.365tcmall.com/home.php?mod=space&uid=49188&do=profile
    when does viagra go generic generic for viagra http://test.tz94.com/home.php?mod=space&uid=335127&do=profile&from=space
    generic viagra usa is there a generic for viagra http://www.wwegames.net/profile/royceo26898
    viagra online no prior prescription cheap viagra online http://www.983sun.com/home.php?mod=space&uid=2445711&do=profile&from=space
    generic viagra online pharmacy cheapest generic viagra http://www.wwegames.net/profile/shonarascoe
    viagra generic generic viagra online http://www.rro.ch/cms/ee/index.php/redirect?url=http://cheapnowaaaa.com/
    viagra online pharmacy viagra generic name http://www.anothercat.com/member.asp?action=view&memName=MavisNeitenstein81
    canadian generic viagra viagra online prescription free http://games4king.com/profile/thanhharp4 ~~““

  8. cheap cialis viagra online cialis online contrassegno https://betadeals.com.ng/user/profile/2447565
    buy cialis online new zealand buy tadalafil cialis online https://betadeals.com.ng/user/profile/2447591
    cialis online contrassegno where to buy cialis online forum http://www.jobref.de/node/1114336
    best online pharmacy no prescription cialis cialis online south africa https://www.navy-net.co.uk/rrpedia/Has_Stress_Received_You_Down_Knock_It_Down_With_These_Terrific_Tips_…_Tip_No._1_From_945
    online canadian pharmacy cialis brand cialis online canada https://www.navy-net.co.uk/rrpedia/Sensation_Stressed_Out_Here_Are_Some_Recommendations_To_Sense_Superior…_Information_No._10_Of_804
    cheap cialis online pharmacy prescription cialis kaufen online https://wiki.cizaro.com/index.php?title=Straightforward_Approaches_To_Make_Tension_A_Detail_Of_The_Past…_Tip_Num_30_Of_760
    cialis 20mg buy online ordering cialis online legal http://armor-team.com/members/mohammedwildin/profile/
    buying cialis online reviews safest online pharmacy for cialis https://betadeals.com.ng/user/profile/2447588
    online pharmacy cialis india cheap daily cialis online https://wiki.cizaro.com/index.php?title=Good_Suggestions_To_Support_You_Deal_With_Worry…_Information_Num_29_Of_572
    online pharmacy cialis best canadian online pharmacy cialis http://www.zoneti.ca/?option=com_k2&view=itemlist&task=user&id=5264373
    order cialis online usa purchase discount cialis online https://crew.ymanage.net/?option=com_k2&view=itemlist&task=user&id=2646942
    buying cialis online yahoo answers farmacia genericos comprar cialis generico online tadalafil precio http://armor-team.com/members/mohammedwildin/profile/
    buy cheap cialis online australia buy cialis online 1 http://bankmitraniaga.co.id/?option=com_k2&view=itemlist&task=user&id=3366811
    purchase cialis online cheap vipps online pharmacy cialis http://www.jobref.de/node/1114192
    best generic cialis online dangers of buying cialis online http://linhkienxe.vn/?option=com_k2&view=itemlist&task=user&id=2924607 ~~““

  9. I know this if off topic but I’m looking into starting my own blog and was curious what all is needed to get set up? I’m assuming having a blog like yours would cost a pretty penny? I’m not very web smart so I’m not 100 certain. Any tips or advice would be greatly appreciated. Cheers

  10. cialis not working anymore cialis generic cost http://samio.se/groups/legal-cialis-online-p36wp5x44ew/
    purchase cialis on line cialis without prescription https://www.liberadi.it/gruppi/create/step/group-cover-image/
    cost of cialis prescription free cialis http://wiznotes.com/UserProfile/tabid/84/userId/147291/Default.aspx
    goodrx cialis cialis doses http://thereichertfoundation.org/UserProfile/tabid/42/userId/11757/Default.aspx
    cialis coupon walmart buying cialis online safe http://omega-air.vn/UserProfile/tabid/134/userId/58/Default.aspx
    how long does it take cialis to work order cialis online http://myfishbook.fr/groupes/cialis-5mg-online-6z1j7ihsbo/
    buying cialis online cialis daily cost https://beardedbook.com/groups/cialis-online-us-pharmacy-79epo66b14/
    cialis generic cost cialis generics http://liberacivitas.altervista.org/gruppi/genuine-cialis-online-pics1ygedz/
    over the counter cialis cialis reddit http://andernach.biz/gruppen/buy-cialis-online-uk-frkgr4th8h/
    cialis active ingredient cialis versus viagra https://dokmusic.org/user-groups/comprare-cialis-online-pnyen6kg2r/
    cialis 5mg daily cialis black https://www.destiny.reisen/gruppi/best-place-to-buy-cialis-online-forum-ugpeh2j4fh/
    liquid cialis cialis wiki https://petagportal.com/gruppen/buy-cialis-online-from-canada-cimoau9n5h/
    cialis vs.viagra reviews how long for cialis to peak http://fbimanufacturedhomes.com/UserProfile/tabid/43/userId/373787/Default.aspx
    cialis discount coupons cialis cheapest price http://escuelalegal.net/UserProfile/tabid/158/userId/3928/Default.aspx
    cialis patent expiration 2016 cialis instructions http://www.erioshop.com/UserProfile/tabid/64/userId/423824/Default.aspx ~~““

发表评论

电子邮件地址不会被公开。