Tag: 常量

“让”如何实现?

我从这个 Apple页面直接拉出了这个例子 struct FixedLengthRange { var firstValue: Int let length: Int } 如果您将此结构的实例分配给常量, let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4) 它说我们不能改变它的属性值,即使它被声明为“var” 这让我想知道如何实现? 我希望在编译时可以检测到任何赋值,并显示编译错误。 但是在上述情况下,为什么它适用于结构的每个属性,而不pipe它是如何定义的? 我试图search这个,发现很难search关键字'let',因为这是相当常见的术语。 任何人都可以帮我理解这个吗?

Objective C具有case / switch的全局常量

有什么办法可以在Objective C中使用全局int常量在case / switch语句中工作吗? 这里的技术(http://stackoverflow.com/questions/538996/constants-in-objective-c)让我访问常量,但不让我把它们放到switch语句中。 在.h FOUNDATION_EXPORT const int UNIT_IDLE; FOUNDATION_EXPORT const int UNIT_DEFEND; 以.m int const UNIT_IDLE = 0; int const UNIT_DEFEND = 1; 错误是“expression式不是整数常量expression式”

从Swift访问在Objective-c .m文件中定义的全局常量CGFloat

我在我的.m文件中定义了一些常量,我需要访问窗体的swift代码。 他们被定义为: const CGFloat testValue = 40.0; 在我的其他objective-c .m文件中,我可以通过使用extern来访问它们: extern const CGFloat testValue 有没有一种等同的方法使这些常量可以从.swift文件访问?

Swift 2未使用的常量警告

我得到一个警告说我的常数没有被使用: 从不使用不可变值'myConst'的初始化; 考虑replace为“_”或删除它 if someVal["value"] != nil { let myConst = someVal["value"] } 那么重命名let myConst = someVal["value"]变成_ myConst = someVal["value"] do / mean是什么意思?

什么时候在Swift中使用静态常量和variables

有一些关于如何在Swift中编写static constant和static variable代码。 但是不清楚何时使用static constant和static variable而不是constant和variable 。 有人可以解释吗?

静态NSDictionary * const的letterValues = @ {…}在一个方法不会编译

在iPhone的文字游戏中 : 我想在我的自定义视图Tile.m中使用下面的代码: – (void)awakeFromNib { [super awakeFromNib]; static NSDictionary* const letterValues = @{ @"A": @1, @"B": @4, @"C": @4, // … @"X": @8, @"Y": @3, @"Z": @10, }; NSString* randomLetter = [kLetters substringWithRange:[kLetters rangeOfComposedCharacterSequenceAtIndex:arc4random_uniform(kLetters.length)]]; int letterValue = [letterValues[randomLetter] integerValue]; _smallLetter.text = _bigLetter.text = randomLetter; _smallValue.text = _bigValue.text = [NSString stringWithFormat:@"%d", letterValue]; } 不幸的是,这给了我编译错误初始化元素不是一个编译时常量 ,我必须删除static关键字来获取我的应用程序在Xcode(这里全屏 […]

从其他常量构build一个常量variables列表

我刚刚读了所有的Objective-C全局常量variables问答,但是我发现它们不适合我的问题。 我需要一个这样的variables列表: NSString *baseURL = @"http://example.org"; NSString *mediaURL = @"http://example.org/media/"; NSString *loginURL = @"http://example.org/login/"; NSString *postURL = @"http://example.org/post/"; etc. 当然,我不能使用这个代码,因为这是一个非常糟糕的方法,如果我需要更改基础url,我必须更改所有variables。 因为我需要从应用程序的每个类访问这些variables,所以我用这种方法将它们声明为全局variables: // Constants.h extern NSString *const baseURL; extern NSString *const mediaURL; extern NSString *const loginURL; extern NSString *const postURL; // Constants.m NSString *const baseURL = @"http://example.org"; NSString *const mediaURL = [NSString stringWithFormat:"%@%@", baseURL, @"/media/"]; NSString […]