2.4 Create a Cipher Application
In this lesson, I will show you how easy it is to write a basic open-source Swift application that you can share with your friends. You can have fun with Swift in just a few lines of code.
1.Introduction2 lessons, 05:56
2.Get Started4 lessons, 32:10
3.Conclusion1 lesson, 01:12
2.4 Create a Cipher Application
Now we're gonna wrap this course up with a little bit of a fun exercise, to be able to create a fun little application, using Swift, on Linux, that you can use with your friends and maybe even expand upon a little bit as you become more familiar, not only with Swift, but also with using it maybe on other platforms as well as on osx. So what I'm going to do here is show you how to complete a simple little application that's gonna be a little cipher application. Now it's going to be fairly simple and there's going to be a couple little spots for you to be able to enhance it and make it a little bit heartier, if you will. But it's going to be pretty cool and it's gonna involve teaching you a couple little constructs within the swift language. So what I’m going to do now is I’m going to create a new swift file call this cipher dot swift so basically the idea here is that I want to be able to create an application on executable on this case now you can use the package manager and you can go ahead and create an application, a more robust version of this if you would like but for now I'm just gonna keep it relatively simple. So, the idea here is that you're going to run this application and you're gonna provide to it two arguments. The first argument is going to be some sort of string or a sentence or something like that and then the second argument is going to be a number of or an integer value which is gonna be represent the number of characters we're going to shift in one direction or the other of your strings. So it's kind of a basic concept behind the Caesar cipher where you're basically shifting the values of characters in one direction or the other. So it's fairly simple, it's not really something I would put any sort of valuable data in, but it could be good for fun with you and your friends. So what I'm gonna do here is I'm going to create a very simple piece of Swift code here. So we're gonna start by saying let arguments, and we're gonna set it equation to process.arguments. So what this is going to do is it's going to take the arguments that you provided via the command line to your application and put them into a constant called arguments. And then at that point I'm going to make the assumption here, now you can do this several different ways. You can check to see how many arguments are provided, the types of the arguments, and really go down that whole path and I'll let you do that, as an exercise for you to play around with Swift and get a little bit more familiar with it. But for now I'm just going to make some naive assumptions that you're providing proper input. So we're going to assume here that the first argument is going to be sentence. Now, in this process dot arguments in this array, the values are stored zero-based, so zero, one, two, three, four, and the first argument is always the name of the executable itself, which is going to be at index zero. So in this case, we're actually concerned about what's going to be in index one. So we're gonna say arguments one, so we're gonna assume that's gonna be our sentence which means if you are providing a sentence, you're gonna have to provide it in double quotes at the command line and if not, you can just leave it as a single word. And then we're going to assume that what we're gonna be shifting by is gonna be the second argument. So this is going to be shift and this is gonna be equal to arguments2. But in this case we want this to be an integer value so we're going to cast this to an int but if you're familiar at all with Swift you're gonna know by trying to do this cast this way It's going to return an optional and so we're going to once again make a naive assumption that we're always going to be passing in an integer as that second value but that may not be the case you may want to put in some checking for this. So in that case since we're going to assume that we're going to unwrap it with an exclamation point. And if some of these things are unfamiliar to you you can definitely go back and check out my Up and Running with Swift Two course and it's going to go into much more detail about these types of constructs within the Swift language. Then we're going to create an array of characters, we'll say var result and we're going to set that equal to, like I said, an array of characters and in this case we're going to initialize it as well so we're going to call it an array of characters and we're going to initialize it as well with the open closed parentheses. So now we have this result that we can work with and append to as a variable, and then these constants here. So all we really want to do at this case is we want to loop through our sentence one character at a time and shift that, shift that value whatever was passed in. So we're going to say four c in sentence dot and we're going to get the utf16 version of all of those characters. So that's going to give us a little bit more leeway and be able to add in some more interesting characters. Now you can limit this a bunch of different ways as scalers, as UTF 1632 8 and all sorts of things like that. I'm gonna keep it fairly simple for this example but you can definitely expand upon this many different ways. So then we're gonna do open and closed curly brackets. And then within here, I am going to simply say, let value be equal to, and we're going to cast into an integer, the value c, and then we're gonna add to that, the value that was passed in for our shift value, so we'll say plus shift, and then at this point, we're gonna say result.append a new character to this, character, and then we're gonna pass into that a Unicode scaler, of that value and then close those two parentheses those three parentheses. Now at this point we have appended a character and we're gonna loop through all the characters there and we're going to create this new result. And then at that point we're simply going to print a new string with the result character array, just like that. And it's as simple as that. So this is once again, very naive, very simple. But I think you'll get the idea. And then eventually you can come in here and maybe expand this to support different protocols, different modular cypher sets, where you can maybe plug and play differently types of cyphers and that could be, maybe an input to this application or something like that, but I'll let your imagination run wild. With this. So at this point, I'm going to go ahead and save this like that. So we'll go ahead and clear this out. Now I want to try to compile this, swift c. I want to try to compile cipher.swift. And it looks like it compiled. And if I go ahead and list the directory here, you now see cipher.swift, as well as our cipher application. So the basic process here is we're going to. Run, cipher and then we need to pass into it two things. A sentence or a string and then a number of values to shift it left or right. So let's just say I put in my first name here, so I said Derek and then I want to shift that five. So now it's going to return Interesting looking string. Now if I were to put that back in here if we did this correctly if I were to run cipher again and I were to type in IJWJP and say minus five I should get back Derek and I do. Now if I want to put in a full sentence I can absolutely do that, but I would have to put this in double quotes, because remember that we're assuming here that the first value is the string and the second value is the number we want to shift by. So if I were to write something like my name and go that way it's going to look at this as my being the first and name being the second and we definitely don't want that. So let's go ahead and say my name is Derek Jenson and we want to shift that nine. And we'll go ahead and run that. So now we've got some sort of crazy business going on in here. And if I wanted to check that I could go ahead and grab this string and I could run my application again. But this time, I wanna shift it minus nine and I wanna replace all of that stuff with what I just generated. And if everything works okay, you'll see that my name is Derek Jensen. So there you have it. A simple little cipher application that you can use by learning a little bit about the swift programming language and then being able to write it on a Mac or Linux or any other supported operating systems or platforms that you can now run the swift programming language on. So, that's pretty cool stuff, something to definitely pay attention to and look forward to as it continues to evolve into the future