Swift扩展示例 [英] Swift extension example

查看:96
本文介绍了Swift扩展示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本来是想知道如何做这样的事情

I was originally wanting to know how to make something like this

UIColor.myCustomGreen

,以便我可以定义自己的颜色并在整个应用程序中使用它们.

so that I could define my own colors and use them throughout my app.

我之前研究过扩展,我认为我可以使用它们来解决问题,但我不记得确切地如何设置扩展.在撰写本文时,在Google上搜索"Swift扩展程序"后,文档 个很长的教程,还有一个无益的堆栈溢出问题.

I had studied extensions before and I thought that I could probably use them to solve my problem, but I couldn't remember exactly how to set extensions up. Searching on Google at the time of this writing for "Swift extension" resulted in the documentation, several long tutorials, and a rather unhelpful Stack Overflow question.

答案是存在的,但是需要对文档和教程进行一些挖掘.我决定写这个问题和以下答案,以便为Stack Overflow添加一些更好的搜索关键字,并提供有关扩展程序设置方式的快速入门.

So the answers are out there, but it takes some digging through the docs and tutorials. I decided to write this question and the following answer to add some better search keywords to Stack Overflow and to provide a quick refresher on how extensions are set up.

我特别想知道:

  • 扩展名驻留在哪里(文件和命名约定)?
  • 扩展语法是什么?
  • 有哪些简单的常用示例?

推荐答案

创建扩展

使用文件>新建>文件...> iOS>源>迅捷文件添加新的迅捷文件.您可以随心所欲地对其进行命名.

Creating an extension

Add a new swift file with File > New > File... > iOS > Source > Swift File. You can call it what you want.

一般的命名约定是将其命名为 TypeName + NewFunctionality.swift .

The general naming convention is to call it TypeName+NewFunctionality.swift.

Double + Conversions.swift

import Swift // or Foundation

extension Double {

    func celsiusToFahrenheit() -> Double {
        return self * 9 / 5 + 32
    }

    func fahrenheitToCelsius() -> Double {
        return (self - 32) * 5 / 9
    }
}

用法:

let boilingPointCelsius = 100.0
let boilingPointFarenheit = boilingPointCelsius.celsiusToFahrenheit()
print(boilingPointFarenheit) // 212.0

示例2- String

String + Shortcuts.swift

import Swift // or Foundation

extension String {

    func replace(target: String, withString: String) -> String {
        return self.replacingOccurrences(of: target, with: withString)
    }
}

用法:

let newString = "the old bike".replace(target: "old", withString: "new")
print(newString) // "the new bike"

此处是一些更常见的String扩展名.

Here are some more common String extensions.

UIColor + CustomColor.swift

import UIKit

extension UIColor {

    class var customGreen: UIColor {
        let darkGreen = 0x008110
        return UIColor.rgb(fromHex: darkGreen)
    }

    class func rgb(fromHex: Int) -> UIColor {

        let red =   CGFloat((fromHex & 0xFF0000) >> 16) / 0xFF
        let green = CGFloat((fromHex & 0x00FF00) >> 8) / 0xFF
        let blue =  CGFloat(fromHex & 0x0000FF) / 0xFF
        let alpha = CGFloat(1.0)

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }
}

另请参见此处.

用法:

view.backgroundColor = UIColor.customGreen

  • 一旦定义了扩展名,它就可以像内置的类函数一样在应用程序中的任何位置使用.
  • 如果不确定确切的功能或属性语法是什么样,可以 Option +单击类似的内置方法.例如,当我 Option +单击UIColor.greenColor时,我看到声明为class func greenColor() -> UIColor.这为我提供了如何设置自定义方法的很好的线索.
  • Apple扩展文档
  • 在Objective-C中,扩展称为类别.
  • Once you define an extension it can be used anywhere in your app just like the built in class functions.
  • If you are not sure of exactly what the function or property syntax should look like, you can Option+click a similar built in method. For example, when I Option+clicked UIColor.greenColor I see the declaration is class func greenColor() -> UIColor. That gives me a good clue for how to set up my custom method.
  • Apple Documentation for Extensions
  • In Objective-C extensions are known as categories.

这篇关于Swift扩展示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆