Learning Swift Attempt #2 - A To-do List App
Hey guys, this is Maads 101, and today I'm going to be trying to make a to-do list app in Swift. So I'm going to go into Xcode, and I'm going to create a new Swift project, and I guess I'll create a single View application. So I'm going to call it Bad To-Do List, and I'll create it. Of course, like always, I'm going to delete all the unit tests because I don't feel like writing those.
Now let's go ahead and immediately try to figure out how on Earth to make a table view. I don't... I'm just going to look for a table view. There's some way to create a table view controller, probably, but I don't know what it is. It's probably real complicated. So here's my table view, and somehow it looks like it's just going to stick everywhere, so that's fine. I'm also going to find a navigation bar for the plus button, and it should be snapping, but I guess not. Yeah, I want the Y to be like 20, and I'll connect these.
Let us just see right now what this app looks like. So if I run it, wow, it really looks just like it does here wide. I'm going to set it back to the correct size. I shall resize this, and you know, it strikes me that there's got to be a way. Right here, this must be it. That must be the way to stick it to the right size, just like it is in Interface Builder on Mac.
So I'm a little bit used to that. Are there any warnings that got generated by that? No, so this looks pretty lousy, but I don't care because this is about learning Swift, not about making a prized-looking to-do list app. So now I find a button. Actually, I think I want a bar button, not a regular button, so let's go ahead and figure out how to delete that. Drag on this button, and I'm going to go ahead and make it...
Well, let's see, how can I make it an add button? There we go! So this to-do list app is already on its way. Now I'm realizing I have no idea how to implement a table view anymore because in Objective C, you implement a protocol for a data source, but here you can't do that. So Swift data source, I think... Oh, I just want this guy's code! UI Table View Data Source—so you just put a comma right after UI View Controller.
What is this? Can I jump to the definition of this? Wow, this is really something! So I'm looking at Apple's official code actually, and it looks like this is a protocol. So I didn't know what Swift protocols were like, but I guess it's just like Objective C protocols. It has to be right because it's Objective C compatible.
Now it's going to be complaining that there's unimplemented methods, but it's not like a good IDE that would just generate the methods for me. I have to look it up. Is there Swift documentation for this? I mean, come on! Where can I get the documentation? Ah, there we go! Here we go; it's a good thing that they show this because I do not know what I would do without it.
So there we go. This is literally just the function declaration. Look how long this is, and I do not know how to implement. Do I just like put these around it? I think so, and you know, if it... Why I don't get why it returns this thing with an exclamation point after it instead of just returning the thing itself. Like, I just don't know, and I guess I have no idea what this is for either.
Oh, you know what? You know what! It must be table view. It's an Objective C method, and so when you put an underscore, that means that the first argument has just the name there. That must be it. Anyway, back to the drawing board. So it's not a pointer; actually, can I just do table view cell? I'll just make it so this is how I would create a table view cell. Usually, I'm going to do...
How do I initialize a UI Table View Cell? So I want to call an Objective C initializer from Swift. So let's see. This is equivalent to... uh, you I init with... I don't even remember what it's called on the actual class. So let's find the Objective C reference and init with style reuse identifier.
Oh jeez, UI Table View Cell style dot... how do I make it so that there’s a check box? I guess default? I want the style to be that, and the reuse identifier to be cell. Is that the write? I forgot the capital T. That would explain why I was getting no suggestions.
So now, did that work? I guess... why can't I just return that as UI Table View Cell? It is a UI Table View Cell! Does this return a question mark or an exclamation point? Oh, it returns an any type! That's why! Any object—oh, that's why! So I had to declare the specific type, and now I need to do as UI Table View Cell.
Ah, wow! How did I figure that? I feel so much smarter than I really am. Okay, now let's just make it say hi, and I know it's still giving me an error because there are another like two methods I have to implement. I have to implement... there's something—forgetting the number of sections and the number of rows! So this will give me the number of sections, which I think I'll always make one, and I definitely do not need the at optional there.
I'm not sure if it's an optional. Yeah, default value is one! Okay, so I can... I don't need that, and the other one! Oh, that's neat! So they really do have a default value there for the number of rows in section! Wow, I still really do not understand why you would use an exclamation point thingy. Like, an implicitly unwrapped value just makes no sense to me.
I guess it's because maybe... you know what it is? It's probably for Objective C compatibility. It's probably so that well—all their library functions, maybe! We'll see! We'll mess around! So this will return one. How about we just return five and make it look like there are five table view cells? Hopefully, we will see five cells.
Oh, Extremus! Hey, yeah! I was asking what this was all about you're making me ask. Oh my goodness gracious! And now that I think about it, this isn't the entire function name! Like, this is part of the name of the function, so I'm not sure how that works—really not sure at all.
Wow, okay! Really having some trouble—where's the iPhone simulator? There it is. So why is there nothing here? I never set the data source of this thing! So the table view's data source needs to be that. Yeah, there we go! Ah, there’s my highs, and of course, when I click on them, they get highlighted and it’s annoying and stupid! Good, okay!
Now, oh boy, we need to make this add button work. I didn't even realize how complicated this would be! So now I need IBAction func addPressed, and I don't care what that is... I don't think so. Let's make this add button do something. No, that's not working. Am I doing the IBAction wrong? There's something I'm missing from last time.
I'm going to open up my code. I'm just going to look at my code from last time. We'll see. We'll find out how I did it. IBAction func buttonTapped. I'm not doing anything wrong, I don't think. Do I need any object exclamation point or question mark or something like that?
Okay, if I just click on this, does it even... It understands that this is a thing! Okay, for some reason that worked— I will never truly understand Xcode, so let's just go ahead. Did Apple ever make an official way to type text in a UI alert view? I think they did!
Ah, okay! So if I do UI alert... alert dot delegate self... Well, okay! I'll worry about that later! I don't even know if self is a keyword in this silly language! Add button with title... okay, I had no idea this was actually a thing, and alert view style equals great that! Yeah, that's how it's done!
Now that's pretty neat actually! I'm liking the enum thing! In fact, I think since I'm assigning to the right type, I can just do that. Yeah, I think I remember explicitly seeing that in some of Apple's code, so that's nice. That is much nicer.
Ah, wow! I just noticed this guy's releasing the thing still—he's using manual reference counting! That's pretty scary! Okay, okay, let's run it! Hit that! This pops up! There's text in it! The keyboard looks just like on Android! Woohoo! Oh, that's kind of nice! Look at that! This has nothing to do with Swift, but I'm just entertained by that.
All right! I should have watched the keynote! I probably would have seen that! For some reason, it says log in there? Yeah, they're not logging in! And of course, the delegate doesn't actually do anything! So now we got to implement that!
So we'll look up... I'll use the documentation here. I assume this is it, so that's nice! So UI alert view delegate! Here we go! What happens if we just print out alert.text? What was it called? Oh no, it's called alert view now! Text field at index z.text, and let's try to print out the button index as well, just to make sure I know what the arguments are.
And I really do not know what that underscore was, and I have no idea what this... I don't need that, so let's go ahead and run it! Hit add, and you can see it pop up! That's a little glitchy, but I'm sure that's Apple's fault, not mine! And nothing popped up because I never set the delegate! Woohoo! Is it self? Can I type self?
And if you're noticing here, I just started noticing there's like a thing that pops up in the middle that says some service died, and then my code goes all black and white. So Apple has their work cut out for them as far as fixing this Xcode, but it's acceptable for non-commercial use like we're using it!
So I hit okay; button index is one when I hit okay. So if button index isn't one, is there no way to do an inline if statement like one line? Wow, if there is no way to do that, I'm going to be so sad! Can I just do... I really need curly braces everywhere? That's so...
All right! If anyone here figures out a way to not have to do these curly braces, please let me know! I would be very pleased to have been able to do that if statement on one line of code! But whatever! So now what I'm going to do— I thought I would create an array of my own custom object that would have like whether it's checked or not, but to be honest, that's going to be too difficult to do in the timeframe of an hour.
I just... I can't see myself doing that! So instead, I'm just going to have an array, and I still have no idea how to do an array! So can I like do that? Let's just look up array Swift! Okay, some goog asking some questions; we'll get some answers! Wow, this really... nothing! I... yeah, here! Here's what I want!
So I can do a typed array! Neat! String! I want an array of strings! I have no idea if I could do like an array of those or like some kind of exclamation pointy kind of array! I don't want to know! I just don't want to know! But this looks fine for now! So what we'll do is tasks.append... uh, alert view text field at index z.text, and I guess that will add, and we'll do table view...re...
What is table? How is it even getting access to table view? What is view? No, I don't want that! Why is view controller a UI view controller? Wait a minute! How has this been happening this entire time? Okay, that makes more sense! It is a UI view controller! I need an IB outlet for this! I need table view, and that's what I want!
I'm not using a table view controller! Let's get that clear! Of course, this isn't a UI Table View Controller! Aha! I just haven't coded for the iPhone in a while! That's the mistake I would have made in Objective C as well! But anyway, so now I have our table view! I can actually access it! I'm going to just rename this to table view underscore just for fun!
Actually, I'm not sure if I can do that! Huh! Okay! Well, there's two different table view variables! Why is it not even giving me an error warning? That concerns me because here's one, and here's one, so they should be conflicting, and the fact that it's not warning me about that is disturbing. But whatever! So reload data, and here I'm going to return, I guess I'll return... oh, I'll return tasks.count!
Do I even need to call? How does this... okay, that's it! I want somehow to understand how arrays work because I know there's magic getter and setter stuff! So maybe there's some magic method? Oh, you can't learn everything in one video! Let's just return the count there! So now it should technically allow us to add things, and I got rid of the high thing.
Actually, what I'm going to do here is I'm going to say this is tasks[indexPath.row] so that it shows the task at the certain index! So there we go, ASDF! And of course, it's not even Dvorak; it's the default iPhone keyboard layout, which is quite disappointing! So it actually is adding our things—That was surprisingly simple!
Now, one last thing we need to do is allow deletion, which is something I don't even remember how to do in Cocoa in general, Cocoa touch, let alone with Swift! So I think we need to do the delegate as well! So let's just get that out of the way—make it the delegate! Okay, and now we'll look up UI Table View Delegate.
Is it just me, or are they repeating things in this documentation? Okay, clearly, I have no idea what I'm doing! I'm just going to Google that and look at the real documentation, even if it is Objective C, just because I'll understand it better! So inserting and deleting can edit row and index path and blah blah blah! So let's just see if I can figure out how to do this myself!
So func table view, table view what is it? Can... and index path? I think I return a bu—I'm not entirely sure! I have no idea what a bu even is! Is it true? I think that's it! That better be it! And what happens if I run it now? If I add ASDF, I cannot swipe it to delete it, huh?
And let's just try one more! We are going to create a method for commit editing style because maybe it... it probably, in fact, that’s what it just said! Didn't to enable swipe to delete, you must implement that! All right, yeah! So yeah, that’s literally just me not reading this thing! Has nothing to do with me not knowing Swift!
So let's go ahead and... these named arguments! I mean, it is exactly the same way pretty much that it was done in Objective C, but it's just so unnatural in this language! I mean, look at this! I have no idea! And what are my options here? UI Table View editing cell editing style dot... so there's delete! That’s what I want!
If it's not delete, I can... I think I can get rid of that now! And what is this supposed to return? Void? So this is something I'm only going to be able to do deletions here! It's giving me an error. Use of undeclared identifier! I forgot to capitalize the T somewhere! You know what? What happens if I get rid of that? Like, I really like... did that change anything? I don't think so!
Maybe... ah! I think what it is is that when there was a pointer in Objective C code, you have to use an exclamation point! But because there's no runtime stuff associated with that, as opposed to the question mark, which has the magic runtime stuff of unpacking it and whatnot! Uh, yeah! We'll just mess around and hope for the best!
Ah, so I have that! I can swipe! I can tap delete! Nothing's really going to happen, is it? Can I set a breakpoint here? This is getting C! Okay, so what am I going to do? Tasks.remove(at: indexPath.row), and what can I do? Table view.beginUpdates, table view.deleteRow(at: indexPath, with: .automatic)!
Ah, this is neat! Oh man! I can just pass that as an array literal with row animation! Unfortunately, it doesn't give me suggestions when I do that! So I have to type this dot and then whatever the thing, and then get rid of the original thing just to get suggestions, but I'm sure that's a feature of Xcode that they will fix!
And now shall it work? I'm going to add two guys! Okay, and wow, it really works! I did not expect this to work so well! I mean, it doesn't work well; it looks disgusting, and it's pretty bad, and of course, I can't type anything because it's QWERTY, and I'm used to typing into Dvorak!
But there we go! It doesn't save; it doesn't do anything, but it lets you add and delete stuff! I learned how to implement these delegate methods! I... you know, I really... still! What happens if I get rid of that? Let's just see what happens! What's the worst that could happen?
Does it still work? Does everything still work? Where was this? The commit editing style? So when I delete, it still works! There's literally no reason to have the exclamation point there! What does that even... I don't even know what that does! Oh yeah, my God! Now we have to look it up! Purpose for exclamation point Swift?
Really, this one letter is driving me nuts! Just one letter! Uh, wrapped implies we should think of an optional value! Oh interesting! So there's optional chaining! What happens? I'm just going to... this is more playing around! Here's what we're going to do: my value equals... so this is my value! It's an INT? It’ll be hey do2 int because we know it's going to not be true!
What happens if I do that right there? Could not... okay, that's an error because I spelled in wrong! See because I'm not using an exclamation point here, I don't think it's going to try to unwrap it! I think it should print out like nil or something like that or empty or whatever here! I don't know what would happen—there'll just be some sort of error!
Well, it printed nil twice! If I do the value + three, it... ah! So the only difference is when an int? and you try to do something with it, it will just automatically screw you over with an invalid operation! Now this saddens me because in Objective C, if you had a nil object, you could just run a bunch of methods on it, and you could... I mean, you would get nil back as a result, but you wouldn't get a runtime error—and I like that!
What happens if I do like... like... uh, what can you do to an integer? If I do like this, is this valid? I just saw this on Stack Overflow! I think like my subconscious just like picked it up! Does this do something? It'll print out nil, but if I do it without the question mark, it'll give me a runtime error!
So you can still do that! This is how it is in CoffeeScript! If you do question mark dot instead of the regular dot and you do it to something that might be nil, and I assume this will work for my value as well, it will either call the method or maybe it won't call! Yeah, okay, so what if I do this? Do I have to do that?
Maybe there's no way to do it? Interesting! So maybe there's no way to change these calls to an INT? which you have to explicitly unwrap, but for an INT!, there is! That would make a whole lot of sense! Okay, so I think I'm coming to understand the exclamation point versus the question mark, and it does make a lot of sense now!
I don't understand if this is a function that’s getting called by Objective C, then if I get rid of this exclamation point and just pass nil to this method from Objective C, some kind of weird stuff is going to happen? Um, so that's funky, but whatever!
So that was interesting—an interesting experiment! And I got a decent, uh, bad looking to-do list app out of it! So anyway, thanks for watching! Thanks for exploring Swift with me! I hope to be making a lot more of these videos because as I look into Swift, I'm going to go look into it with you guys!
Because this is really a good channel for learning! So thanks for watching Maads 101! Subscribe and goodbye!