Menu
CoddyTech

Protocols and Conformance

Lesson 33 of 48 in Coddy's Swift for Dart Developers course.

Every Dart class introduces an interface, and implements requires an implementation of its members. An abstract interface class can explicitly define a contract. Swift uses a protocol to declare requirements; structs, enums and classes can conform. A getter requirement allows either a computed getter or a readable stored property. Generic constraints let a function operate on any conforming type without assuming a particular concrete implementation. Swift any ProtocolName is an existential value that can hold a conformer; a generic parameter describes a type chosen for a call. The example asks a named object to supply a label.

Complete Dart program. Each labelled block is a separate file.

Named.dart

abstract interface class Named {
  String get label;
}

Folder.dart

import 'Named.dart';
class Folder implements Named {
  @override
  final String label;
  Folder(this.label);
}

main.dart

import 'Folder.dart';
void main() {
  print(Folder('archive').label.toUpperCase());
}

Complete Swift program. Each labelled block is a separate file.

Named.swift

import Foundation
protocol Named { var label: String { get } }

Folder.swift

import Foundation
struct Folder: Named { let label: String }

Functions.swift

import Foundation
func heading<T: Named>(_ item: T) -> String { item.label.uppercased() }

main.swift

import Foundation
print(heading(Folder(label: "archive")))
challenge icon

Challenge

Easy

Implement protocol Rated requiring rating: Int { get }. Implement Review: Rated with stored rating and init(rating:). Implement generic totalRating<T: Rated>(_ items: [T]) -> Int in Functions.swift to sum every rating, including those of other conforming types. Input has 0 through 50 ratings 0 through 5; output the sum.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Rated.swift, Review.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
struct ProbeRated: Rated { let rating: Int }
let custom = ProbeRated(rating: 4); return totalRating([custom, ProbeRated(rating: 2)]) == 6 && totalRating([Review(rating: 3)]) == 3 && totalRating([Review]()) == 0
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for Dart Developers

Practice on your own: Swift playground