斯坦福 iOS7 公开课第二课: Xcode 5
05 Feb 2014
这是 Stanford CS193p: Developing iOS 7 Apps for iPhone and iPad Fall 2013-14 文字版,按照视频字幕手工编辑完成。这里的内容由于是按照讲话的录音记录下来的,所以文字风格偏口语化,也比较”线性”. 视频的内容大部分会记录成文字,但也酌情省略了可以忽略的语句。
本文图片较多,如果图片的注解足够清晰明了,就会省去文字记录,想要看到文章更好的排版效果,可直接浏览 GitHub 里的文件。
####Today
Today we’re going to have some slides at the beginning, little more talking, and then I’m going to have a quite big demo that’s going to try and hopefully synthesize all the things I’ve been talking about on the slides for the first two lectures, which is that we’re going to start building our card game. This card matching game is going to be our substrate for the first two weeks of learning some Objective-C, learning about Xcode, learning about how iOS hooks up the controller, and the view, and the model to make a UI.
If you remember from the last time, we did the card thing, it was a very simple class. Today, we’re going to go on and do another class, which is a deck! The deck of cards. And remember, that card and deck are generic; they’re not specific to playing cards. A playing card, like the ace of clubs(梅花 A) or the king of the hearts(红心国王)
####Objective-C Demo
- A getter is really just setting and getting a value. It might have side effects, like setting it might update the UI, or getting it might make sure it’s initialized first.
- If you want to have a optional argument, the only way to do that in Objective-C is to delcare a new method. So just understand that, in some languages, some arguments can be optional or you can kind of overload things to have the same method name have different arguments. No. In Objective-C every method is completely distinct and has a distinct name.
- Our deck is just going to contain a bunch of cards.
- Objects in array can be of any class. There’s really no way to specify what kind of objct is in an array. Some languages allow you to do that. You can specify “This is an array of strings” and it knows that. But in Objective-C you can’t do that. That’s a little bit of the wild west of Objective-C. but there are ways to kind of check and see what the objects are if you want to be really safe about it. Normally an NS array is immutable. Oncd it’s created, whatever objects enter it, that’s the objects that are in it forever. So if we want array where we can add stuff, we have to use its subclass of NS array called NS mutable array.
- Insert object add index(insertObject:atIndex:) is a method in NS mutable array, not in NS array – only in NS mutable array because that would be mutating it – that inserts the object at that index in the array and index zero is going to be the top that we’re going to define. And then otherwise if we’re not going to put it at the top, we’re going to put it at the bottom, we’re going to use a different NS mutable array method called add object, and that just adds something at the end of the array.
- arc4random() is a C library function. It gets a random integer.
- Let’s move onto another class, which is playing card. The reason I’m showing you playing cards, i just want to to show you what it looks like to make a subclass of another class that you’ve written. So playing card is subclass of card. And this is the specific card like king of hearts, three of diamonds. Now, it has properties that are specific to a playing card, which is the suit and rank. The rank being like a three, four, a jack, king; and the suit being hearts, diamonds, clubs.The four French playing card suits used primarily in the English speaking world: spades (♠), hearts (♥), diamonds (♦) and clubs (♣).
- And here I’m using notice NSU integer instead of unsigned int. So NSU integer and unsigned int are almost exactly the same thing. The only thing about NSU integer is it’s typedef. It might be a little different on different platforms. For example, the new iPhone 5s are 64-bit processers. So NSU integer is going to be a 64-bit int, unsigned int on an iPhone 5. And it might only about a 32-bit one back on an iPhone 4 and before.
- Notice that our rank is really nice because if our rank is zero, which it starts out being when we say new playing card, all the instance variables are zero so rank would be zero, we get this nice question mark. But our suit starts out as nil, and it would be nice if the suit also returned question mark if it was unsaid, if it was nil. So here I’m just overriding the getter of suit to say: “If return, if my suit is nill, then return the question mark, otherwise when my suit’s not nil, then return what the suit is.”
- Every time I use the at sign open square bracket–the blue one – to create an array, that’s actually creating a new array every time. That at sign square bracket and all this array stuff is really just calling methods. That’s calling a method like
alloc
init
with array with objects or something like that.
- The only thing we really use plus methods for, class methods, is two things really: Creating things like in the previous slide when we had string with format that’s a class method that was creating a string for us; and then also utility methods like this, like the return constants and things that, you know, our class wants and utility methods.
- method
maxRank
looks at how many strings are in rank strings.
- Never call that alloc thing without wrapping an init around it. That makes on snse to have an object allocated in the heap that’s never been initialized. And vice versa: Never call that init except for when you wrap it around an alloc. And dfinitely never call that init more than once.
- (instancetype)init
: init
is always going to return self. instancetype is new for iOS 7.
- A playing card deck has 52 cards in it, one of each kind of card.
Okay, so that’s it for the slides.
###Let’s start building a Card Game out of these classes.
The following slides are a walkthrough of the demonstration done in class. You will need this walkthrough to do your first homework assignment.
This is the Slides.
注意:上面链接的演示项目用到的材料可在这里找到:https://github.com/m2mtech/matchismo-2013-14
关于 CS193P 的 Assignment Solutions 的资料,这里比较齐全:http://cs193p.m2m.at/
Some Notes on the above Xcode 5 Demonstration Slides:
-
“Document Outline” shows you all the instances of objects in your view in a common outline form. So you can really easily find them and their relationships to each other.
-
It’s a very important mechanism–Use Autoayout, which was introduced in iOS 6 and vastly improved in iOS 7, especially in Xcode–that allows when your user interface changes size, like it goes from being an iPhone 4 to an iPhone 5, or it rotates from being vertical to being horizontal, or it’s on an iPad in a bigger sapce. For all the buttons and evertying to kind of move to a new position that makes sense or at least most of them. And then maybe you might have to move a few of them by hand. depending what’s going on. But it automatically lays it out. And this is really important because I’m sure devices will continue tobe differing sizes as new devices come out over the years. The blue guidelines appeared on the Main.storyboard while dragging things, are the number one most important things for auto layout. But getting those blue guidelines, making sure you drop things with at least one blue guideline somewhere is really important to kind of get you going down the right path with the properly auto laid out UI.
-
While editing Button
, we’re editing an instance of a button, and we’re actually editing live objects. They’re essentially going to be freezed dried. And when your app runs, add water, they come to lifem, with their attributes set, and all the sizes and positions, and all their auto layout information in the object. In some systems you layout the button, you set the size, and you set the attributes. And then, you know, behind the scenes a bunch of code is being generated to create the button. That’s not what we’re doing here. So it’s a little different than you might be used to some other systems.
###Coming Up
#####Needs more Card Game!
- Your homework will be to have that single card flip through an entire Deck of PlayingCards.
- Next week we’ll make multiple cards and put in logic to match them against each other.
#####Also next week …
- Objective-C language in depth
- Foundation classes: arrays, dictionaries, strings, etc.
- Dynamic vs. static typing
- Protocols, categories and much, much more!
####Lecture 2 done!