gpt4 book ai didi

swift - 在 Swift 中,协议(protocol)扩展是否允许函数体?

转载 作者:搜寻专家 更新时间:2023-11-01 06:27:11 25 4
gpt4 key购买 nike

我正在阅读一个教程,我注意到作者扩展了他们的名为 Activity 的协议(protocol),并在他们的代码中编写了函数的主体。这确实可以编译,但是我的印象是协议(protocol)只显示方法签名,或者如果它确实实现了主体,那么它将是一个变异函数。下面的代码没有对其功能之一使用变异,但它仍然可以运行并且可以工作!有人可以解释这种现象或确认协议(protocol)扩展可以有方法体吗?

import CareKit
import SwiftyJSON

enum ActivityType: String {
case Intervention
case Assessment
}

enum ScheduleType: String {
case Weekly
case Daily

}

enum StepFormat : String {
case Scale
case Quantity
}

protocol Activity {

var identifier : String { get set}
var groupIdentifier : String { get set}
var title : String { get set}
var colour : UIColor? { get set}
var text : String { get set}
var startDate : Date { get set}
var schedule : [NSNumber] { get set}
var scheduleType : ScheduleType { get set}
var instructions : String? { get set}
var imageURL : NSURL? { get set}
var activityType: ActivityType { get set}
var medication : Medication? { get set}

init()
init(json: JSON)
func createCareKitActivity() -> OCKCarePlanActivity

}

extension Activity {

// A mutating function to allow Acticities or Assessments to intialiser base properties
mutating func parseActivityFields(json: JSON) {
self.identifier = json["identifier"].string!
self.groupIdentifier = json["group_identifier"].string!
self.title = json["title"].string!
self.text = json["text"].string!

let colourString = json["color"].string!
self.colour = UIColor.colorWithString(colourString)

if let instructionString = json["instructions"].string {
self.instructions = instructionString
}

if let imageString = json["imageURL"].string {
let componentsOfString = imageString.components(separatedBy: ".")

if let pathForResource = Bundle.main.path(forResource: componentsOfString[0], ofType: componentsOfString[1]){
self.imageURL = NSURL(fileURLWithPath: pathForResource)
}
}

self.startDate = dateFromString(string: json["startdate"].string!)!
self.scheduleType = ScheduleType(rawValue: json["scheduletype"].string!)!

self.schedule = json["schedule"].string!.components(separatedBy: ",").map ( {
NSNumber(value: Int32($0)!)
})

if let medication = json["medication"].string,
let medicationImageString = json["medicationimage"].string {

let componentsOfString = medicationImageString.components(separatedBy: ".")
let pathForResource = Bundle.main.path(forResource: componentsOfString[0], ofType: componentsOfString[1])

self.medication = Medication.init(medication: medication, imageURL: NSURL.init(fileURLWithPath: pathForResource!))
}

}

init(json: JSON) {

self.init()

self.parseActivityFields(json: json)

}


func createCareKitActivity() -> OCKCarePlanActivity{

//creates a schedule based on the internal values for start and end dates
let startDateComponents = NSDateComponents(date: self.startDate, calendar: NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)! as Calendar)

let activitySchedule: OCKCareSchedule!

switch self.scheduleType {
case .Weekly :
activitySchedule = OCKCareSchedule.weeklySchedule(withStartDate: startDateComponents as DateComponents, occurrencesOnEachDay: self.schedule)

case .Daily:
activitySchedule = OCKCareSchedule.dailySchedule(withStartDate: startDateComponents as DateComponents, occurrencesPerDay: self.schedule[0].uintValue)

}

let activity = OCKCarePlanActivity.intervention(
withIdentifier: identifier,
groupIdentifier: nil,
title: title,
text: text,
tintColor: colour,
instructions: instructions,
imageURL: imageURL as? URL,
schedule: activitySchedule,
userInfo: ["medication": medication], optional: false)

return activity

}
}

最佳答案

在 Swift 中,扩展允许您为协议(protocol)提供默认实现。
根据关于协议(protocol)的 Swift 文档,

Protocols can be extended to provide method, initializer, subscript, and computed property implementations to conforming types. This allows you to define behavior on protocols themselves, rather than in each type’s individual conformance or in a global function.

来源:Swift Documentation

关于swift - 在 Swift 中,协议(protocol)扩展是否允许函数体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com