如何在Swift中存储对整数的引用

时间:2015-08-08 22:16:21

标签: swift reference-type

我知道swift有引用类型和值类型。我知道Int是一种价值类型。但是如何存储对整数的引用呢?

this.Invoke(new Action(() => {this.UpdateUserList();}));

我尝试使用盒装类型,我尝试使用Int数组,但是它们都不能用于保存对整数的引用。

我想我可以写自己的

var x:Int = 1
var y:Int = x   // I want y to reference x (not copy)
++y
println(x)     // prints 1, but I want 2

似乎有点尴尬。

2 个答案:

答案 0 :(得分:5)

不幸的是,在Swift中没有引用类型jest.dontMock('../server/isomorphic/stores/TrendingSocialStore.js'); jest.dontMock('../server/isomorphic/constants/AppEvent.js'); jest.dontMock('../server/isomorphic/dispatchers/AppDispatcher.js'); //jest.mock('../server/isomorphic/dispatchers/AppDispatcher.js'); TrendingSocialStore = require('../server/isomorphic/stores/TrendingSocialStore.js'); var AppDispatcher; var AppEventConstants; var AppEventConstants = require('../server/isomorphic/constants/AppEvent.js'); var AppDispatcher = require('../server/isomorphic/dispatchers/AppDispatcher.js'); console.log(AppDispatcher.register.mock) //var callback = AppDispatcher.register.mock.calls[0][0] trending = [{ type: 'sponsored', business: { name: 'Lorem Ipsum', img: 'http://placehold.it/256x256', category: 'Coffee' }, sponsor: { name: 'YP', copy: 'Ad by YP<sup>SM</sup>' } }]; location = { id: 1234, name: 'University City, CA', searchPath: 'university-city', desc: 'University City\'s economy is anchored by the University of California, San Diego campus which forms the north part of the community. Southern areas of University City are composed of a mix of single family homes, a retirement community, and a few condominiums. The north-central area, known as the "UTC" area is composed of a dense mix of condominiums and apartment complexes.', region: { name: 'California', abbr: 'CA' } }; // mock actions var actionInit = { actionType: AppEventConstants.INIT, data: { trending: trending, location: location } }; describe('TrendingSocialStore', function() { it('registers callback with the dispatcher', function() { expect(AppDispatcher.register.mock.calls.length).toBe(1); }); it('grabs trending and location data', function() { callback(actionInit); }); }); 或类似的东西,所以你必须自己创建一个Box-Type。

例如通用的:

Integer

答案 1 :(得分:-1)

您也可以使用闭包。我认为这些更好,因为它们更强大,也更具体。它们不仅存储引用,还指示如何使用引用。例如,

var x:Int = 1
var setx = { (a:Int) in x = a }
var getx = { x }
setx(getx() + 1)
println(x)             // This will print 2

我不建议实际定义getx / setx。定义一个闭包,为您的应用程序执行特定任务。