Machine Learning in Swift: Core ML, Create ML, and TensorFlow Lite Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Machine Learning in Swift: Libraries and Tools for iOS/macOS



Development for the Apple ecosystem is traditionally associated with the Swift language. Despite Python remaining the undisputed leader in Data Science, Swift is actively catching up, offering native solutions optimized for Apple hardware. In this article, we will break down the key libraries and frameworks that allow you to integrate machine learning (ML) into applications for iOS, macOS, watchOS, and tvOS, as well as train models directly on the device.



1. Core ML — The Foundation



Core ML is not just a library, but a fundamental framework from Apple that serves as the runtime for executing ML models on the device. It supports models trained in Python (scikit-learn, TensorFlow, PyTorch) and converted to the .mlmodel or .mlpackage format using Core ML Tools.



Key capabilities of Core ML:

  • Optimization for Neural Engine, GPU, and CPU (automatic selection).
  • Support for images, text, sequences, tabular data.
  • Work with classification models, object detection (YOLO, SSD), semantic segmentation.
  • Support for transformer models (BERT, GPT-2) via Natural Language.


Example of integrating a Core ML model for image classification (Swift):



import CoreMLimport Vision

// 1. Load the model (e.g., MobileNetV2)guard let model = try? VNCoreMLModel(for: MobileNetV2(configuration: MLModelConfiguration()).model) else { fatalError("Failed to load model")}

// 2. Create a Vision requestlet request = VNCoreMLRequest(model: model) { request, error in guard let results = request.results as? [VNClassificationObservation], let topResult = results.first else { return } print("Recognized: \\(topResult.identifier) with confidence \\(topResult.confidence)")}

// 3. Execute the request on the imageguard let image = UIImage(named: "cat.jpg"), let ciImage = CIImage(image: image) else { return }

let handler = VNImageRequestHandler(ciImage: ciImage, options: [:])try? handler.perform([request])


2. Create ML — Training Without Code (and With Code)



Create ML is an Apple tool built into Xcode and Swift Playgrounds, allowing you to train machine learning models directly on a Mac using Swift. It is ideal for prototyping and creating custom models without leaving the ecosystem.



Create ML supports:

  • Classification and regression (tabular data).
  • Image classification and object detection.
  • Text analysis (sentiment, categorization).
  • Recommendation systems (based on user behavior).
  • Sound classification.


Example of training a text classifier in Swift (macOS):



import CreateMLimport Foundation

// 1. Load data from CSVlet data = try MLDataTable(contentsOf: URL(fileURLWithPath: "/Users/me/emails.csv"))

// 2. Split into train/testlet (trainingData, testingData) = data.randomSplit(by: 0.8, seed: 42)

// 3. Train a sentiment classifierlet sentimentClassifier = try MLTextClassifier(trainingData: trainingData, textColumn: "text", labelColumn: "label")

// 4. Evaluate accuracylet evaluationMetrics = sentimentClassifier.evaluation(on: testingData)print("Accuracy: \\(evaluationMetrics.classificationError)")

// 5. Save the modellet metadata = MLModelMetadata(author: "PythonLib", shortDescription: "Email sentiment classifier", version: "1.0")try sentimentClassifier.write(to: URL(fileURLW

Recommendations