简体   繁体   English

如何在swift中定义常量?

[英]How to define constant in swift?

I searched a lot over the internet to find the right solution to convert my constant .h file to swift. 我在互联网上搜索了很多,找到了将我的常量.h文件转换为swift的正确解决方案。 I already tried these links 我已经尝试过这些链接了

  1. how to use a objective-c define from swift 如何使用swift中的objective-c定义
  2. Globalconstants file in swift Globalconstants文件在swift中

These both are use full but not completely solved my problem. 这两个都是使用完整,但没有完全解决我的问题。

I just want to convert these three #define to swift all other can be converted in the same way as these will do. 我只想将这三个#define转换为swift所有其他的转换方式与这些方法相同。

  1. #define IS_IPHONE_4S [[UIScreen mainScreen] bounds].size.height == 480
  2. #define IOS7VERSION ([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0?YES:NO)
  3. #define RGBCOLOR(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]

You can use the keyword let for this. 您可以使用关键字let来实现此目的。 For example: 例如:

let IsIPhone4S = UIScreen.mainScreen().bounds.height == 480
let IOS7Version = Float(UIDevice.currentDevice().systemVersion) >= 7

And for the last case you should use function: 对于最后一种情况,你应该使用函数:

func RGBColor(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
    return UIColor(red: red / 255, green: green / 255, blue: blue / 255, alpha: 1)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM