当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Swift ExpressibleByIntegerLiteral用法及代码示例


协议

ExpressibleByIntegerLiteral

可以用整数文字初始化的类型。

声明

protocol ExpressibleByIntegerLiteral

概述

标准库整数和浮点类型,例如 IntDouble ,符合 ExpressibleByIntegerLiteral 协议。您可以通过分配一个整数文字来初始化任何这些类型的变量或常量。


// Type inferred as 'Int'
let cookieCount = 12


// An array of 'Int'
let chipsPerCookie = [21, 22, 25, 23, 24, 19]


// A floating-point value initialized using an integer literal
let redPercentage: Double = 1
// redPercentage == 1.0

符合ExpressibleByIntegerLiteral

要将ExpressibleByIntegerLiteral 一致性添加到您的自定义类型,请实现所需的初始化程序。

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

相关用法


注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 ExpressibleByIntegerLiteral。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。