我IModelIdProvider在MyProjectName文件夹中有一个swift协议.我创建了一个RemoteModelFactoryTest在MyProjectNameTests文件夹中命名的objective-c单元测试类.要做一些测试, RemoteModelFactoryTest必须实现IModelIdProvider协议.
当我#import "MyProjectName-Swift.h"在Objective-c类中使用我的swift协议时,我得到了一个file not found.
如果我将#import指令更改为#import "MyProjectNameTests-Swift.h",则会找到标题但不是我的协议(它在MyProjectName项目中定义,而不是在MyProjectNameTests项目中定义).
在*Tests使用Swift代码的项目中有什么特别的事吗?
我正在尝试做一个嵌套的递归函数,但是当我编译时,编译器崩溃(Segmentation fault).
这是我的代码:
func test()
{
func inner(val : Int)
{
println("\(val)")
if val > 0
{
inner(val - 1)
}
}
inner(3)
}
Run Code Online (Sandbox Code Playgroud)
编译器日志在这里
我尝试使用类的enumerateGroupsWithTypes方法,ALAssetsLibrary但我得到第一个参数的错误.
func enumerateGroupsWithTypes(types: ALAssetsGroupType,
usingBlock enumerationBlock: ALAssetsLibraryGroupsEnumerationResultsBlock!,
failureBlock: ALAssetsLibraryAccessFailureBlock!)
Run Code Online (Sandbox Code Playgroud)
assetLib.enumerateGroupsWithTypes(ALAssetsGroupAll, usingBlock: success, failureBlock: fail)
Run Code Online (Sandbox Code Playgroud)
但是我收到编译错误 'CUnsignedInt' is not convertible to 'ALAssetsGroupType'
基于我在互联网上发现的和我自己的测试,我也尝试过
assetLib.enumerateGroupsWithTypes(ALAssetsGroupAll as ALAssetsGroupType, usingBlock: success, failureBlock: fail)
Run Code Online (Sandbox Code Playgroud)
结果是编译错误 Cannot convert the expression's type 'Void' to type 'ALAssetsGroupType'
assetLib.enumerateGroupsWithTypes(ALAssetsGroupType(ALAssetsGroupAll), usingBlock: success, failureBlock: fail)
Run Code Online (Sandbox Code Playgroud)
结果是运行时错误EXC_BAD_ACCESS和XCode崩溃.
我正在使用PINCache将缓存添加到我的应用中,并且处于缓存系统调用编码/解码的委托方法的情况。这些方法是通用的,但通用值未明确符合Codable。因为它们是委托,所以我无法更改签名以使泛型类型符合Codable。
func modelForKey<T : SimpleModel>(_ cacheKey: String?, context: Any?, completion: @escaping (T?, NSError?) -> ()) {
guard let cacheKey = cacheKey, let data = cache.object(forKey: cacheKey) as? Data, T.self is Codable else {
completion(nil, nil)
return
}
let decoder = JSONDecoder()
do {
let model: T = try decoder.decode(T.self, from: data)
completion(model, nil)
} catch {
completion(nil, nil)
}
}
Run Code Online (Sandbox Code Playgroud)
使用此代码,我遇到以下错误:
在参数类型中
T.Type,T与预期类型不符Decodable
如何强迫我decoder接受通用价值?
我正在开发一个带有文本字段的应用程序.在这个字段中写的文本将被打印,我有一些问题,如表情符号,中文字符等...因为字体不提供这些字符.
这就是为什么我想获得字体提供的所有字符(字体被下载所以我可以直接处理文件或UIFont对象).
我听说过,CTFontGetGlyphsForCharacters但我不确定这个功能是否符合我的要求,我无法让它发挥作用.
这是我的代码:
CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, NULL);
NSString *characters = @""; // emoji character
NSUInteger count = characters.length;
CGGlyph glyphs[count];
if (CTFontGetGlyphsForCharacters(fontRef, (const unichar*)[characters cStringUsingEncoding:NSUTF8StringEncoding], glyphs, count) == false)
NSLog(@"CTFontGetGlyphsForCharacters failed.");
Run Code Online (Sandbox Code Playgroud)
这里CTFontGetGlyphsForCharacters返回false.这就是我想要的,因为所使用的字体不提供字符''.
问题是,当我更换NSString *characters = @""的NSString *characters = @"abc",CTFontGetGlyphsForCharacters返回再假.显然,我的字体为所有ASCII字符提供了一个字形.
当我写这个:
// json is of type AnyObject
var status : String? = json.valueForKeyPath("status") as String?
Run Code Online (Sandbox Code Playgroud)
Xcode 在编译过程中似乎被无限循环阻塞:

我的代码有问题吗?
我做了一些测试,当我写道:
var status : String? = json.valueForKeyPath("status") as? String
Run Code Online (Sandbox Code Playgroud)
Xcode 能够编译,但是什么时候valueForKeyPath返回nil.
我正在尝试UICollectionView用一个由上面UIImageView和UIView上面组成的自定义单元格做一个简单的操作.
如果未选择UIView单元格,则单元格顶部的backgroundColor属性设置为UIColor(red: 1, green: 1, blue: 1, alpha: 0.5).
我对选择有疑问.collectionView:didSelectItemAtIndexPath:调用该方法但是当我改变UIView之前描述的方法时,没有任何事情发生.
这是我的代码collectionView:
class TestCollectionViewController: UICollectionViewController
{
var items = [1, 2, 3]
let cellId = "Cell"
override func viewDidLoad()
{
self.collectionView.allowsMultipleSelection = true
self.collectionView.delaysContentTouches = false
}
override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int
{
return items.count
}
override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell!
{
var cell = …Run Code Online (Sandbox Code Playgroud) 我正在使用 prawn 创建一个 pdf 文件。在某些情况下,生成后,我需要将pdf方向从横向更改为纵向。
大虾提供了一个rotate功能,但它不适用于整个pdf,并且由于某些原因,旋转必须在生成之后/结束时完成。
我用 ImageMagick 找到了这个解决方案,但我也面临着巨大的质量下降(并且没有提供解决方案)。
我还尝试使用我生成的pdf作为新虾pdf的模板并用于:page_layout更改方向:
pdf = Prawn::Document.new(page_layout: :landscape, template: file_name)
pdf.render
Run Code Online (Sandbox Code Playgroud)
但该template参数似乎被忽略,它只是创建一个空白的 pdf。
我正在寻找一个好的解决方案(如果我不需要外部库,那就加分)。