Xcode - free Swift example code
About 5 min
Xcode - free Swift example code êŽë š
Xcode - free Swift example code
Learn Swift coding for iOS with these free tutorials â learn Swift, iOS, and Xcode
Found 19 articles in the Swift Knowledge Base for this category.
How to add conditions to a breakpoint
When you place a regular breakpoint on a line of code, Xcode will pause at that point when itâs reached. However, sometimes you need something a little more fine-grained: perhaps you want the breakpoint to be ignored the first few times itâs hit, or perhaps you want execution to be paused only when a certain condition is true.
How to add markers to the jump bar
Xcodeâs jump bar â the popup menu describing all properties and methods in the current class, activated using Ctrl-6 â is a simple way to navigate around your file, particularly when you know you can just type a few letters to automatically filter the list.
How to create a project using Swift Package Manager
Swift Package Manager is a tool that generates Swift projects then manages dependencies for them, and itâs commonly used if want to build your projects without Xcode. If you have Xcode installed then you already have the Swift Package Manager, but if youâre using Linux it will come with the Swift for Linux package from https://swift.org/.
How to create exception breakpoints in Xcode
Exception breakpoints are a powerful debugging tool that remarkably few people know about, so please read the following carefully and put it into practice!
How to debug view layouts in Xcode
View debugging lets you visualize exactly how your app is drawing to the screen by exploding your UI into 3D. So, if you're sure you added a button but you just can't see it, view debugging is for you: you can spin your interface around inside Xcode, and you'll probably find your button lurking behind another view because of a bug.
How to fix the error âView controller is unreachable because it has no entry points and no identifier for runtime accessâ
All view controllers must be accessible somehow, and any that arenât will cause Xcode to throw up a warning: âView Controller is unreachable because it has no entry points, and no identifier for runtime access.â There are three ways you can fix this:
How to load assets from Xcode asset catalogs
Xcode asset catalogs are a smart and efficient way to bring together your artwork in a single place. But they are also optimized for performance: when your app is built, your assets converted to an optimized binary format for faster loading, so they are recommended for all kinds of apps unless you have a specific reason to avoid them. (Note: SpriteKit games should texture atlases if possible.
How to lock Interface Builder controls to stop accidental changes
Interface Builder is the standard tool for making iOS interfaces using drag and drop, but it does make it remarkably easy to make a mistake â moving a view by accident, adjusting a property with a typo, or perhaps embedding one view inside another by dragging.
How to make Xcode play sounds while debugging
Xcode has lots of tools to help you debug user interface problems, but itâs a little tricker to debug things you canât see â network requests failing, data loading code going wrong, and so on.
How to render example content using prepareForInterfaceBuilder()
Whenever you create a custom
UIView
subclass using @IBDesignable
, itâs usually a good idea to provide it with some sample content so it can render meaningfully at design time.How to repeat code when debugging using the instruction pointer
Have you ever been debugging a problem in your code and really wished you could repeat a chunk of code without having to start from scratch?
How to set the clock in the iOS Simulator
By default the iOS Simulator shows whatever the time is on your Mac, but you can use Xcodeâs
simctl
command to override that with a custom time.How to use storyboard references to simplify your storyboards
Storyboards deliver useful features, such as being able to see a lot of your user interface side-by-side and being able to design static cell designs for your table and collection views. However, they can also cause havoc with teams, because two developers attempting to modify the storyboard at the same time will immediately hit source control problems.
How to use vector images in your asset catalog
Xcode lets you use vector images for all your artwork, which saves you having to worry about retina artwork, super retina artwork, and whatever Apple dreams up in the future. Even better, using this vector artwork has no performance cost â iOS and Xcode do all the work for you,
How to used a named UIColor in code and Interface Builder
Since Xcode 9.0 weâve been able to create named colors directly inside asset catalogs, which means itâs possible to select a precise color once then use it uniformly across all code and storyboards.
What are breakpoints?
Breakpoints are locations in your code where Xcode will pause so that you can inspect the state of your program. They donât have any impact on the final performance of your code â in fact, they donât even exist in your code at all â so thereâs no harm using them liberally.
What are Swift error breakpoints?
Xcode has a few special breakpoint types, accessed by going to the breakpoint navigator (Cmd+8) then clicking + in the bottom-left corner. One of the options in that menu is Swift Error Breakpoint, and it allows Xcode to pause when Swift errors are thrown.
What are watchpoints?
Youâre probably already familiar with Xcodeâs breakpoints, which allow you to pause code when a certain point is reached. Watchpoints also let you pause code, but their job is to pause when a variable is changed anywhere else in your code â itâs like a property observer, except implemented entirely using Xcodeâs debugger.
What is an IBOutlet?
When you connect your storyboard to your code Xcode inserts two special markers:
@IBAction
and @IBOutlet
. Both of these effectively do nothing when you build your app, but they are still important because Xcode uses them to understand which of your properties and methods are relevant to Interface Builder.