当前位置:网站首页>Use of swift extension
Use of swift extension
2022-04-22 20:29:00 【Autumn trifles】
//
// ExtensionsProtocols.swift
// SwiftCode
//
// Created by Alisa on 2022/4/14.
// Copyright 2022 Alisa. All rights reserved.
//
import Foundation
/*
Extension is used to add new functions to existing data types , The protocol is used to contract attributes and methods for the implementation of data types that comply with it
** Expand
stay oc There are categories in the syntax , Its function is to add new functions to existing classes , stay swift You can use extensions to achieve similar functions , Different from categories , Category has name , The extension has no name
Expand supported functions , as follows :
<1> Add calculation properties
<2> Defining instance methods and class methods
<3> Define a new construction method
<4> Define subscript method
<5> Define nested methods
<6> Make an existing type conform to the protocol
<7> Extend the protocol 、 Add a new property or method Convention
Expand the use of keywords extension
*/
class Mirror{
var color:String
var purpose:String
var mirrorSize:Float = 0.0
init() {
self.color = ""
self.purpose = ""
}
}
extension Mirror{
// Extend a calculated property
var size:Float{
set{
self.mirrorSize = newValue
}
get{
return self.mirrorSize
}
}
// Extend an instance method
func showTheBrightLight(){
print("The mirror show this bright light, Sunny weather !")
}
// Extend a class method
class func cleanMirror(){
print(" Clean it this mirror")
}
// Extend a new construction method
convenience init(color:String){
self.init()
self.color = color
}
// Extension cannot add ordinary attributes , Only calculation properties can be added
//var size:Float //Extensions must not contain stored properties
}
protocol BreadTalking{
func showTheBreadName()->Void
}
class BreadFat{
var color:String
var name:String
init() {
self.color = "yellow"
self.name = ""
}
}
extension BreadFat:BreadTalking{
func showTheBreadName() {
print("This Bread name is: \(self.name)")
}
}
// Extend the existing types of the system , Change yourself
extension Int{
// The modification itself requires the use of mutating
mutating func change(){
self = self * self
}
}
class Extensions{
// Use the extension to add calculation properties 、 Example method 、 Class method 、 Construction method ( Convenient construction method )
func useExtensions(){
let mirror = Mirror(color: "Pink")
mirror.size = 45.0 // Use extended calculation properties
mirror.showTheBrightLight() // Use the extended instance method
// Use extended class methods
Mirror.cleanMirror()
/* Print information :
The mirror show this bright light, Sunny weather ! Use the extended instance method
Clean it this mirror Use extended class methods
*/
}
// Use extensions to add protocols to existing classes
func useExtentionForProtocl(){
let bread = BreadFat()
bread.name = "Buttered Toast"
// A method implemented using an extended accepted protocol
bread.showTheBreadName()
/* Print information
This Bread name is: Buttered Toast
*/
}
// Use extensions to change the instance itself
func useExtentionForInt(){
var value = 3
value.change()
print("current value is: \(value)")
/* Print information
current value is: 9
*/
}
}
版权声明
本文为[Autumn trifles]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222017444160.html
边栏推荐
- DA14580作为server发送数据
- Adobe系列错误代码解决方案汇总
- ZTNA (Zero Trust Network Access)
- 华为机考题汇总
- Displays the difference between an implementation interface and an implicit implementation interface
- 专访木瓜移动刘凡:木瓜移动如何为行业聚能?
- 显示实现接口和隐式实现接口的区别
- Filebeat
- SCI/SSCI期刊列表已更新,这几本期刊被剔除~
- Mysql, in the unique index of combination, handles the problem of null value
猜你喜欢
随机推荐
PHP 零基础入门笔记(11):字符串 String
ZTNA (Zero Trust Network Access)
October's Android interview failed miserably in byte three, and fortunately won Xiaomi offer
如何让机器人更像“人”,让slam更灵活?
东吴证券X袋鼠云:数据轻松可取、毫秒级反应能力,东吴证券做对了什么?
Swift 协议的使用
【建议收藏】面试没亮点
分库分表&百亿级数据迁移
Review of SSM framework
D trigger in FPGA
Virtual machine building and installation pulsar environment tutorial (for development and testing)
Team work principles
CDH6.3.2 启用Kerberos 认证
Use of swift protocol
Operation and maintenance (33) centos7 6 deploy kubernetes cluster through kubedm
06. Refactoring - simplifying conditional expressions
未授权访问漏洞总结
Chapter 2 array
Leetcode daily question 396 Rotation function
Timestamp conversion









