struct Box{
var count: Int = 1
}
var boxOne = Box()
var boxTwo = boxOne
boxTwo.count = 2
print(boxOne.count)
print(boxTwo.count)
var language = "Kotlin"
let closure = { print("I like \(language)!")}
language = "Swift"
closure()
-enable-upcoming-feature ExistentialAny
オプションを有効にした場合、下記のコードはコンパイルできなくなります。どこでエラーが発生するでしょう?struct QuizView: View {
let quiz: Quiz // (A)
var body: some View { ... }
}
protocol Quiz { // (B)
var question: String { get }
var choice: [String] { get }
}
struct FourChoiceQuiz: Quiz { ... } // (C)
swift
を実行すると何が起こるでしょう。$ swift
func count() {
let numbers = (1...100)
.lazy
.filter {
Thread.sleep(forTimeInterval: 1)
return $0.isMultiple(of: 2)
}
for num in numbers {
if num == 6 {
print("finished")
return
}
}
}
var x = 10
func increment(_ number: inout Int) {
number += 1
x += 1
}
increment(&x)
print(x)
@MainActor
final class ArticlesViewModel: ObservableObject {
private let repository = ArticleRepository()
@Published private(set) var articles: [Article] = []
func load() async {
do {
let articles = try await repository.articles()
self.articles = articles // A
} catch {
// Handle error
}
}
}
actor ArticleRepository {
func articles() async throws -> [Article] {
let articles = try await API.getArticles(count: 20)
return articles // B
}
}
struct TabScene: View {
enum Item: String, CaseIterable {
case one, two, three, four, five, six
}
var body: some View {
TabView {
ForEach(Item.allCases, id: .self) { item in
Text(item.rawValue)
.tabItem { Label(item.rawValue, systemImage: "star") }
}
}
}
}
// (A)
struct StructNode {
init(body: String, parent: StructNode?) {
self.body = body
self.parent = parent
}
let body: String
let parent: StructNode?
}
// (B)
class ClassNode {
init(body: String, parent: ClassNode?) {
self.body = body
self.parent = parent
}
let body: String
let parent: ClassNode?
}
import OrderedCollections
let s1: Set = [false, true, true]
let s2: Set = [true, true, false]
print(s1 == s2)
let os1: OrderedSet = [false, true, true]
let os2: OrderedSet = [true, true, false]
print(os1 == os2)