24 Xcode Tips in 15 Minutes

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
sponsored by instabug who have an sdk to help you minimize debugging time by providing you with complete device details network logs and reproduction steps with every bug report find out more at instabug.com hello my name is paul and in this video i'm going to walk you through 24 xcode tips in about 15 minutes it's going to be extremely fast so let's get straight to the tips in swift all structs get a member wise initializer synthesized directly by the compiler to make them easier to create this here is a class though so you won't get one got to make your own initializer fortunately xcode can help select the class name then go to the editor menu and choose refactor then choose generate memberwise initializer boom it'll do all the work for you you can then move that wherever you want it i prefer mine below the property list like that and you're done when building software it's important to make sure your app works for everyone and xcode has lots of tools in place to help you do just that for example while your app's running you can go down here and choose environment overrides this will override parts of the way the simulator works live while you watch for example i might say let's override the font with a larger dynamic type size you see it adapts immediately so i can see exactly how it's going to look even at very very large sizes or you could say i want to turn on bold text and perhaps also button shapes and again you can see exactly how that looks right here live in the simulator there is an option for toggling between light mode and dark mode but often a quicker version to do that is to have this image selected and just press shift command a toggle the appearance live you probably already know that you can click after a brace like here or here to see the matching brace highlighted but another great tip is to find any brace you want and double click it for example this one here that'll select the entire block of code that belongs to those braces as well as checking your codes correct xcode can also check your spelling is correct and it's really smart too for example i could have hello world i can instead say hello rolled a typo and of course i can get away with that just fine but if you go to the edit menu and choose format then spelling and grammar you can now select check spelling while typing it'll highlight for me world is wrong it should be world like i said this is super smart if you had a property for example let example property name equals false obviously that's not a real word but it can tell the camel case capital p capital n it'll understand the separate words whereas if i had a lowercase p it would be example property now i'd highlight being incorrect if you've got a swift file like this one with multiple errors inside of course you can go through every single one individually and press fix again and again and again alternatively for lots of errors you might find it easier to go to the editor menu and choose fix all issues have xcode apply the fixits across the board when you do a search using xcode's find navigator all the results line up here on the left of your window you then click on any one of these things to review the code and make any amendments you want to but when you're done i recommend you press the backspace key to remove the search item from the results so you know exactly which ones you reviewed so far and which ones you still have to review this is such a simple xcode tip but it will save you time if you don't already know it often when editing swift ui code you'll see this automatic preview updating paused you can of course resume this by pressing the resume button up here but the shortcut's really easy and really useful it is option command p and it acts like the resume button and it'll pop up straight away whenever you want it or if you're like me and you prefer not to have the canvas up most of the time you can hide it with option command return like that it can be annoying having to regularly move from simulator back to xcode to simulator to xcode and so on fortunately there's a really simple fix with the simulator selected choose the window menu then choose tile window to right of screen and then select xcode on the left you then resize this down to fit how much space it needs and you're done they'll both stay side by side the entire time xcode has great co-completion built right into it you just go ahead and press dot and then type some letters for example access however sometimes as you scroll through you might find the names are quite long for example here you'll see they're clipped at the end the ellipsis fortunately you actually just grab this window on the side here and pull it out as big or small as you'd like to read the full thing break points are a critical tool in our debugging toolkit and you can place them anywhere by left clicking on the line number in the gutter area then to remove it right click and choose delete break point alternatively left click to place then click and drag to remove but the method i prefer is much simpler just on the line you're on right now press command backslash to toggle a breakpoint on or off for that line the best way to write stable software is to write great tests but sometimes that's not enough you see sometimes the output from one test can affect the input to another test and then you've got real problems there's no clean slate anymore to fix this excise got a simple solution go to the product menu hold down the option key and then choose test in this box here look for the info tab then press options button then press randomize execution order and now it'll run your tests in a different order every time to make sure they don't accidentally depend on each other when you're working in a long file and you want to navigate around quickly you should use the jump bar it's up here let's press this and it'll show you all the methods and properties in one list including dividers and any markers you've placed if it's too long for you just start typing to get a filter for example i could type review here to see only things that have review somewhere in the name and this type to filter thing works in the other bars as well for example over here in the simulator selection area i can start typing i could say show me only pro model devices we're halfway through this video now so i just want to say if you're enjoying it subscribe to my channel i make lots more videos like this one teaching you swift swift ui ui kit and more anyway back to the tips when browsing through someone else's swift code it's common to want to get an overview of how the thing works rather than getting bogged down in lots and lots of lines of code well excel couldn't do that for you just go to the file you want press control command add up and it will generate a synthesized interface for that file showing things like the enum keys the properties any comments and any method names with no bodies of those methods just the signatures themselves even better you can press ctrl command up again and go to another related file in this case it's my tests so you can browse around these related files very very easily to get a good overview of how the code actually works xcode has two powerful shortcuts for working with comments the first one is nice and simple just go ahead and select some code and then press command and slash to comment the code or command slash again to uncomment the code the second one is for documentation comments move your cursor before any method then press option command slash and it will generate a documentation comment for that method pulling out the parameter names argument and completion here for you to fill in as your projects grow in size it can be increasingly hard to use a project navigator to find the exact files you want there are two ways to fix this the first one is to right click on any group you want and then choose sort these files by their name and it will result that group alphabetically the second one is down here in the filter box just type a few letters for example battery and it'll show only files that match that search xcode does a great job of handling interactive source control changes right inside its editor to try it out go to the xcode menu then choose preferences go to the source control tab and make sure show source control changes is checked once that's done go ahead and make your change for example here i might say i want to have the quality of service of user interactive a change and now here in the left you'll see live 55 has got a blue bar next to it saying this line's changed but there's more you can click on this blue bar and select show change and i'll show you before and after a live div of your code and update as you add more code to it every new line will come along in blue like that xcode has a mini map for browsing around longer swift files effectively you can enable it by going to the editor menu then choosing minimap and one of the cool things it does is as you hover your mouse over it it'll tell you the name of the method you're looking at in the minimap so here's executes completion down here's execute subject and so forth we can also hold down the command key to reveal all the things inside the mini-map and then just click on one to jump straight to it it's very common to write code that fails tests particularly when using tdd fortunately xcode has a special shortcut in place to re-run your previous test and only that one the shortcuts this control option command and g it'll run just the previous test and not any others very very fast the writer john reed who blogs at qualitycoding.org calls us a very memorable name smash go control option command g if you're like me you'll rely very heavily on xcode's keyboard shortcuts but it's very easy to make mistakes for example you might try and go for open quickly which is shift command o but by accident you press shift command p and get page setup which is useful for the never times you want to print out your source code similarly command p for printing again never going to happen fortunately you can replace these with better shortcuts go to xcodes menu and choose preferences then go to the key bindings tab and use a filter box to search for the ones you want to change for example we looked at page setup so i'll select this one here then press backspace to delete that command entirely and we looked at print again i search for print here it is here command p i press backspace and it's zapped gone no more keyboard shortcut for printing no more accidental key presses however that shortcut's now free for more important things for example you might use swift ui a lot and say the canvas i want to refresh out more easily and right now it's option command p i can replace that by selecting the keyboard shortcut and saying instead you're now promoted to command p fine navigator is a brilliant tool for helping you find and replace text inside your project all these titles up here can be clicked on to be changed for example this is fine right now it could also be replaced this is text right now it could be references or regular expressions or call hierarchy or more we have containing could be matching or starting or ending with stuff there's another great feature here which is found by clicking on the magnifying glass icon it'll show you all recent searches and you can select any one of these things to show the results immediately when you're trying to solve a problem in your project and you take some code from hacking with swift or stack overflow or somewhere else you might find code like this in your project no indenting at all fortunately xcode and fixes with a single keyboard shortcut just select the code then press ctrl i and xcode will reindent it correctly you can test in-app purchases right inside xcode and the simulator first press command-n to make a new file then search for storekit configuration file and press next and save it wherever you want it when this file opens go ahead and press plus to make a new file then choose the kind of in-app purchase you want for example i'll choose a non-consumable one you're gonna name this thing i'll call this thing premium unlock give it an id i'll use com.hackingwithswift.premium and a price and check whether family sharing should work or not then when you've done that go ahead to the product menu hold down option and choose run you now see some options here in the options tab and you want to choose store configuration change that from none to be your new configuration and now if testing in a simulator any in-app purchase calls will use that test file directly rather than going through app store connect there are many build settings to customize the way xcode builds your project but it can be hard to remember what they all do fortunately nearly all of them are documented to find out what one does just select it for example enable bit code then go to the quick help inspector on the right of xcodes window and you'll see a full description here alternatively go to the thing hold down option and double click and a window will appear directly over the option with the same text xcode's canvas is a great way to preview your swift ui layouts easily but it's less good when your views are split across multiple files for example here i've got a list of 100 rows and each one is a numbered row view a separate view now let's say i want to see what my list looks like with the row font being larger i might start doing that over a numbered row.swift but now my preview has changed so i'm a different file fortunately xcode can do better than this go back to your previous view the one you want to keep on screen then look for down below the preview this little thumbtack little pin image at the very bottom there and now that preview is pinned in place and will not change which means i can go back to numbered row.swift and make changes and that preview won't move i'll say for example dot font dot large title and i'll preview the previous view with my change right there and that's it all 24 xcode tips in just over 15 minutes but now it's your turn what's your favorite tip to get the most from xcode let me know in the comments below and don't forget to subscribe to the channel if you enjoy this video and want to learn more about xcode swift swift ui and much more
Info
Channel: Paul Hudson
Views: 42,348
Rating: 4.9857192 out of 5
Keywords: Xcode, tips, tricks, techniques, swift, swiftui, ui, testing, search
Id: 8kiR_QsQNPc
Channel Id: undefined
Length: 15min 30sec (930 seconds)
Published: Fri Feb 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.