The iOS Apprentice : Getting Sta

作者: J_Knight_ | 来源:发表于2016-03-05 23:59 被阅读1087次

原来在CSDN更新过自己曾经在自学iOS过程中整理的学习笔记,但是正式入坑4个月了一直没有更新(罪过啊),今日开始慢慢整合两边的博客,记录自己的学习进程和反思总结,还有就是闲暇之余整理的读书笔记。

荒废了近4个月的博客,打算从The iOS Apprentice系列开始。本系列在自学过程中也略有耳闻,但是没有学习。入坑之后,我老大建议学习此系列,因此就在下班后研究研究,发现果然名不虚传。

The iOS Apprentice

在这里强烈推荐给各位坑友,而且觉得自己英语不够过硬的坑友们不要害怕,本系列虽然是全英文,但是语法并不难,我相信有四级基础的人读起来都没有什么问题。

作为笔者学习笔记的开篇,需要声明一下:本人的学习笔记并不是完全依照原文的讲述顺序,内容也夹杂着自己的想法和见解,因此本笔记并不代表原教程。而且开发步骤也不全,只有笔者觉得比较重要的几个地方,所以看笔者的笔记是写不出完整的项目的,但是如果想向笔者要源代码是可以的。

开始了:本系列的第一部:Getting Started通过制作一个小游戏的全部过程,讲述了iOS开发的过程和基本知识。

Bull‘s Eye

View Controller


我们用VC用来管理app的某单个界面,有三种文件构成了VC :the .xib, .h and .m。

  • the xib file:UI界面的构成 (LOOK)
  • the .h file:告诉电脑VC是做什么的 (WHAT)
  • the .m file:告诉电脑VC怎么做 (HOW)

Objects, messages and methods


  • 对象:可以拥有数据和功能。
  • 例子:按钮的数据:按钮的名字;按钮的功能:点击事件,触发某方法。
  • 某个对象可以“看到”其他对象的数据,也可以要求其他对象执行方法。
对象间消息的发送

local variables vs. instance variables


变量可以让app记住东西,而且每个变量有它自己的生命期,生命期的长短取决于在哪里定义变量。

1. local variables(局部变量):

生命周期的终结=所在方法生命周期的终结。

int difference = . . . ;
int points = . . . ;
NSString *message = . . . ;
UIAlertView *alertView = . . . ;

局部变量在方法之外无法使用,方法结束后,局部变量不会存在。下次调用此方法时,局部变量会成为新的局部变量。我的理解是申请新的一块内存。

2. instance variables(实例变量):

生命周期的终结=所属对象生命周期的终结。

实例变量的声明:
- (void)viewDidLoad
{
[super viewDidLoad];
currentValue = 50;
}
The viewDidLoad 的方法在VC加载之后,现实之前。所以适合在此时初始化实例变量。

方法的封装:


如果需要在两处以上的地方使用同一代码,就需要封装起来,这样使得程序看起来逻辑清晰,更加优雅。

封装之前:出现重复代码

- (void)viewDidLoad
{
  [super viewDidLoad];
  targetValue = 1 + (arc4random() % 100);
  currentValue = 50;
  self.slider.value = currentValue;
}


- (IBAction)showAlert
{
  [alertView show];
  targetValue = 1 + (arc4random() % 100);
  currentValue = 50;
  self.slider.value = currentValue;
}

方法封装:将重复代码封装起来

- (void)startNewRound
{
  targetValue = 1 + (arc4random() % 100);
  currentValue = 50;
  self.slider.value = currentValue;
}

封装后:代码清晰简洁

- (void)viewDidLoad
{
  [super viewDidLoad];
  [self startNewRound];
}

- (IBAction)showAlert
{
  [alertView show];
  [self startNewRound];
}

观察者模式


本程序到最后有一个优化的点:在用户点击关闭弹窗按钮后才开始下一轮游戏。那么VC就需要监听到此按钮的点击,这里就利用到了代理。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger )buttonIndex
{
  [self startNewRound];
  [self updateLabels];
}

作者提到的编程习惯


Whenever I start working on a new app, I first make a list of all the different pieces of functionality I think the app will need. This becomes my programming to-do list.

大意:写程序之前写一份清单,弄清自己要做什么。

By cutting up the workload into small steps you make the project less daunting — you can always find a step that is simple and small enough to make a good starting point and take it from there.

大意:分解任务,总有一个小任务可以作为项目的开始。

NSLog is a great help to show you what is going on in the app. We used it to verify that we properly hooked up the action to the slider and that we can read its value as the slider is moved. I often use NSLog to make sure my apps are doing the right thing before I add more functionality.

大意:输出语句可以帮助我们确保程序运行正确。

最后送上作者的话,都是我特别喜欢的

(不想影响原文的优雅,就不翻译了)

There is a big difference between knowing the path and walking the path — the only way to learn programming is to do it.

It’s only a small detail but the difference between a mediocre app and a great app is that great apps do all the small details right.
Well-written source code speaks for itself.

The best use for comment lines is to explain how your code works. Well-written source code is self- explanatory but sometimes additional clarification is useful.

总结

整体来说,本教程“麻雀虽小,五脏俱全”,虽然极其简单,但是讲述了制作一个app所需要的基本步骤和基本思想,是初学者学习极佳资料。

-------------------------------- 2018年7月16日更新 --------------------------------

注意注意!!!

笔者在近期开通了个人公众号,主要分享编程,读书笔记,思考类的文章。

  • 编程类文章:包括笔者以前发布的精选技术文章,以及后续发布的技术文章(以原创为主),并且逐渐脱离 iOS 的内容,将侧重点会转移到提高编程能力的方向上。
  • 读书笔记类文章:分享编程类思考类心理类职场类书籍的读书笔记。
  • 思考类文章:分享笔者平时在技术上生活上的思考。

因为公众号每天发布的消息数有限制,所以到目前为止还没有将所有过去的精选文章都发布在公众号上,后续会逐步发布的。

而且因为各大博客平台的各种限制,后面还会在公众号上发布一些短小精干,以小见大的干货文章哦~

扫下方的公众号二维码并点击关注,期待与您的共同成长~

公众号:程序员维他命

相关文章

网友评论

    本文标题:The iOS Apprentice : Getting Sta

    本文链接:https://www.haomeiwen.com/subject/kkhakttx.html