I am working on an app for OS X 10.9 with swift, sandboxed. The app needs access to a SQLite database file. I let the user choose/open a file with NSOpenPanel. I then save the file path with NSUserDefaults for later use. I want this file to be opened automatically every time when the app is started again. In the Project Navigator you will find all files and groups that Xcode creates by default for a Cocoa App. What is new here (comparing again to iOS projects), is an.entitlements file. This is a property list file that contains rights an app is granted with. Most of the times there is no need to edit it manually.
- Database Swift App Macos High Sierra
- Database Swift App Macos Download
- Database Swift App Macos App
- Database Swift App Macos Sync
The Missing Manual
for Swift Development
The Guide I Wish I Had When I Started Out
Join 20,000+ Developers Learning About Swift Development
Download Your Free CopyIn the previous tutorial, you learned about the Core Data stack and the classes involved. In this tutorial, we take a close look at Xcode's template for Core Data applications.
Setting Up the Project
Open Xcode and select New > Project.. from the File menu. Choose the Single View Application template from the iOS > Application section and click Next.
Name the application Core Data, enter an organization name and identifier, and set Language to Swift. Check the Use Core Data checkbox to make sure Xcode adds the necessary files and code for working with Core Data. Personal assistant san antonio.
![Database swift app macos installer Database swift app macos installer](/uploads/1/3/4/2/134256578/564224056.png)
Exploring Files and Folders
The project includes a file that may be new to you, Core_Data.xcdatamodeld. This is the data model I talked about in the previous tutorial. Select the file to take a quick peek at its contents.
Because the data model doesn't contain any entities yet, there isn't much to see. We take a closer look at the data model and the data model editor in Mastering Core Data With Swift 3. Best free first person shooter games.
Exploring the Application Delegate
More interesting is the implementation of the
AppDelegate
class. Open AppDelegate.swift and take a moment to see what Xcode has added for us when we checked the Use Core Data checkbox during the project setup. The first detail worth noting is the import statement for the Core Data framework at the top.The other interesting bits are the lazy stored property,
persistentContainer
, of type NSPersistentContainer
and the saveContext()
helper method.Xcode's Core Data template received a dramatic makeover with the release of the
NSPersistentContainer
class. This class was introduced alongside iOS 10, tvOS 10, macOS 10.12, and watchOS 3. While it greatly simplifies setting up Core Data, it isn't ideal for learning the framework. Why is that?The
NSPersistentContainer
class encapsulates the Core Data stack. It is in charge of setting up the Core Data stack and managing the building blocks we discussed in the previous tutorial. This means that we don't have any insight into the inner workings of the NSPersistentContainer
class, which makes it difficult to illustrate what we learned about the Core Data stack in the previous tutorial.Fortunately, the
NSPersistentContainer
class provides access to the managed object model, the managed object context, and the persistent store coordinator. In Mastering Core Data With Swift 3, we take a different approach by building a Core Data stack from scratch. This teaches you essential insights into the inner workings of the Core Data stack, the heart of every Core Data application. If you are serious about learning and using Core Data, then you should take Mastering Core Data With Swift 3. It also discuss the NSPersistentContainer
class in more detail.This is what the lazy property looks like. Comments are omitted for clarity.
The
NSPersistentContainer
instance is initialized by invoking a convenience initializer, init(name:)
, passing in the name of the data model, Core_Data. Notice that there is no explicit instantiation of NSManagedObjectModel
, NSPersistentStoreCoordinator
, or NSManagedObjectContext
. This is abstracted away by the NSPersistentContainer
class.A Core Data stack without a persistent store isn't terribly useful. After initializing the persistent container, the persistent store is loaded by invoking the
loadPersistentStores(completionHandler:)
method. It accepts one parameter, a closure, which is invoked when loading of the persistent store is completed, successfully or unsuccessfully. The closure accepts two parameters, an instance of the NSPersistentStoreDescription
class and an optional Error
object.The
NSPersistentStoreDescription
class is another new addition to the Core Data framework. It encapsulates the information and configuration to add a persistent store to the persistent store coordinator. As the name of the class implies, it describes a persistent store.What Is a Persistent Container?
As I already mentioned, the
NSPersistentContainer
class encapsulates the Core Data stack and it is available as of iOS 10, tvOS 10, macOS 10.12, and watchOS 3. It makes setting up a Core Data stack easier than ever, providing developers with a simple setup with some flexibility.After creating an instance of the
NSPersistentContainer
class, you can easily access the managed object model and the persistent store coordinator through the managedObjectModel
and persistentStoreCoordinator
properties.The class also provides easy access to a
viewContext
property, an instance of the NSManagedObjectContext
class that operates on the main queue. As the name of the property implies, this managed object context is designed to be used in combination with the application's user interface. Main queue? User interface? Don't worry about this for now. We discuss this in more detail in Mastering Core Data With Swift 3.There are several other neat features, such as the ability to create background managed object contexts and specifying the directory in which the persistent store lives by subclassing
NSPersistentContainer
. These are more advanced features.Saving Changes
The application template also includes a helper method to save the changes of the managed object context. In this method, we access the managed object context of the persistent container through the
viewContext
property.We check if the managed object context has any changes by inspecting the value of the
hasChanges
property. To save the changes of the managed object context, we invoke save()
on the managed object context. Because this method is throwing, we wrap it in a do-catch
statement.Refactoring the Core Data Stack
I always try to keep the implementation of the application delegate short and simple. Setting up the Core Data stack is something we could and should move to a dedicated class. This makes the implementation of the
AppDelegate
class shorter and less cluttered. This is something we discuss in detail in Mastering Core Data With Swift.Questions? Leave them in the comments below or reach out to me on Twitter. You can download the source files for this article from GitHub.
Now that you know what Core Data is and how the Core Data stack is set up, it's time to write some code. If you're serious about Core Data, check out Mastering Core Data With Swift. We build an application that is powered by Core Data and you learn everything you need to know to use Core Data in your own projects.
Download Your Free Copy ofThe Missing Manual
for Swift Development
The Guide I Wish I Had When I Started Out
Join 20,000+ Developers Learning About Swift Development
Download Your Free CopyThis article was originally written back in the end of 2015. The source code is deprecated and not compatible with current Swift versions, so I removed it.
If you want to learn how to make a network connection between your devices using Bonjour discovery service you are on the right place. In this post I am going to show you the basics, so for example you will be able to make a remote controller from your watch or iOS device in order to send data directly to any tvOS or macOS machines.
Multi-platform development
If you want to create an app that supports multiple platforms, you might want to target macOS, iOS, watchOS, tvOS and soon Linux as well. The code snippet below is going to help you detecting the current platform that you are working with.
Network connection 101
Bonjour discovery service
Bonjour, also known as zero-configuration networking, enables automatic discovery of devices and services on a local network using industry standard IP protocols.
Database Swift App Macos High Sierra
So basically with Bonjour you can find network devices on your local network. This comes very handy if you are trying to figure out the list of devices that are connected to your LAN. Using NetService class will help you to detect all the devices with the available services that they support. The whole Bonjour API is relatively small and well-written. From the aspect of server side you just have to create the NetService object, and listen to the incoming connections through the NetServiceDelegate.
You have to be on the same WiFi network with all devices / simulators.
TCP connection
TCP provides reliable, ordered, and error-checked delivery of a stream of octets (bytes) between applications running on hosts communicating by an IP network.
With the help of TCP you can build up a reliable network connection. There is a Stream class in Foundation to help you sending data back and forth between devices. If you have a working connection form NetServiceDelegate, just listen to the stream events to handle incoming data through the StreamDelegate. I don't want to go into the details, just download the example code and check it out for yourself.
UDP connection
With UDP, computer applications can send messages, in this case referred to as datagrams, to other hosts on an Internet Protocol (IP) network.
If you look at the article about UDP you'll clearly see that the main difference from TCP is that this protocol will not guarantee you safety of the data delivery. Data may arrives unordered or duplicated, it's your task to handle these scenarios, but the UDP is fast. If you want to build a file transfer app you should definitely go with TCP, but for example controlling a real-time action game UDP is just as good enough.
CocoaAsyncSocket
Database Swift App Macos Download
This library is the absolute winner for me and probably it is the best option for everyone who wants to set up a network connection really quickly, because it requires way less code than implementing delegates. Of course you'll still need the Bonjour layer above the whole thing, but that's just fine to deal with.
If you are using CocoaAsyncSocket you will see that the API is straightforward, only after 5 minutes I could relatively easily figure it out what's going on and I was able to send messages through the network. It supports all the Apple platforms, you can seamlessly integrate it using Carthage or CocoaPods.
CoreBluetooth APIs
I was not really familiar with the CoreBluetooth framework API's, that's the reason why I basically just followed and ported this tutsplus.com code example to Swift 4. Honestly I felt that the API is somehow overcomplicated with all those messy delegate functions. If I have to chose between CoreBluetooth or CocoaAsyncSocket, I'd go with the last one. So yes, obviously I am not a bluetooth expert, but this little project was a good first impression about how things are working inside the CB framework.
Database Swift App Macos App
WatchConnectivity framework
Database Swift App Macos Sync
If you want to communicate between iOS and watchOS you'll probably use the WatchConnectivity framework, especially the WKSession class. It's really not so complicated, with just a few lines of code you can send messages form the watch to the iPhone. You can read this tutorial, but if you download my final sources for this article, you'll find almost the same thing inside the package.