小编dop*_*pcn的帖子

变量'xxx'从未变异,考虑改为'让'

更新到xcode7-beta我遇到了一种新的警告.这是我的代码

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var attributes: [UICollectionViewLayoutAttributes]? = super.layoutAttributesForElementsInRect(rect)
    if let layoutInfo = self.layoutInfo {
        attributes?.append(layoutInfo)
    }
    return attributes
}
Run Code Online (Sandbox Code Playgroud)

警告信息是 Variable 'attributes' was never mutated, consider changing to 'let' constant

xcode为什么这么说Variable 'attributes' was never mutated

问题更新

当我将代码更改为此时,警告消失了

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var attributes: [UICollectionViewLayoutAttributes]? = super.layoutAttributesForElementsInRect(rect)
    if let layoutInfo = self.layoutInfo {
        attributes!.append(layoutInfo)
    }
    return attributes
}
Run Code Online (Sandbox Code Playgroud)

所以强行打开可以把它带走.但它可能不是一件好事吗?

optional swift

15
推荐指数
2
解决办法
2万
查看次数

ios默认在xcode中新创建swift文件导入cocoa

我正在开发一个iOS应用程序,并希望现在可以在swift中构建新功能.但是当我通过Xcode-> File-> New-> File-> Cocoa Class-> Language Swift创建一个新文件时,新创建的文件总是import Cocoa代替UIKit.

这是你们所有人的默认行为吗?有没有办法在每次创建新的swift文件时更改导入UIKit?

更新:感谢所有答案.我犯了一个可怕的错误

xcode ios swift

6
推荐指数
1
解决办法
2977
查看次数

swift错误的String init(格式...方法?

let elem1 = "1"
let elem2 = "2"
let array = [elem1, elem2]
let format = "%@ != %@"

//compiler error
//can't find an initializer for type...
let str = String(format: format, arguments: elem1, elem2)

//no errors but wrong output
//("%@ != %@", "1", "2")
let str = String(format: format, _arguments: elem1, elem2)

//runtime error
//fatal error: can't unsafeBitCast between types of different sizes
//this is what I need
let str = String(format: format, arguments: array)

//only this works with …
Run Code Online (Sandbox Code Playgroud)

string swift

5
推荐指数
1
解决办法
3197
查看次数

xcode 通过 ruby​​ gem xcodeproj 更改代码签名身份

我对 Ruby 完全陌生,但设法在 xcode 中更改了基于项目的代码签名身份和配置文件,如下所示:

#!/usr/bin/env ruby

require 'xcodeproj'

xcproj = Xcodeproj::Project.open("MyProject.xcodeproj")

xcproj.build_configurations.each do |item|
    item.build_settings['CODE_SIGN_IDENTITY[sdk=iphoneos*]'] = "iOS Development: xxxxxx xxxx (xxxxxxxxx)"
end

xcproj.build_configurations.each do |item|
    item.build_settings['PROVISIONING_PROFILE[sdk=iphoneos*]'] = "628352b1-9b78-xxxx-xxxx-xxxxxxxxx"
end

xcproj.save
Run Code Online (Sandbox Code Playgroud)

我的问题是基于目标的代码签名身份和配置文件将覆盖基于项目的身份和配置文件。但是我找不到直接设置基于目标的方法。希望有人可以在这里提供帮助。谢谢

ruby xcode ios xcode-project

5
推荐指数
1
解决办法
947
查看次数

这个GCD实现了getter setter线程安全并且比@synchronized更好吗?objc

@interface ViewController ()
@property (nonatomic, strong) NSString *someString;
@end

@implementation ViewController

@synthesize someString = _someString;

- (NSString *)someString {
    __block NSString *tmp;
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        tmp = _someString;
    });
    return tmp;
}

- (void)setSomeString:(NSString *)someString {
    __block NSString *tmp;
    dispatch_barrier_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        tmp = someString;
    });
    _someString = tmp;
}
@end
Run Code Online (Sandbox Code Playgroud)

有人说它比@synchronized方式更好,因为所有的锁定都是在GCD中处理的.

multithreading objective-c thread-safety grand-central-dispatch ios

3
推荐指数
1
解决办法
1502
查看次数