小编kei*_*ter的帖子

符合协议和类的Swift属性

@property (strong, nonatomic) UIViewController<UITableViewDelegate> *thing;
Run Code Online (Sandbox Code Playgroud)

我想在Swift中实现这个Objective-C代码中的属性.所以这就是我尝试过的:

class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
    var thing: T!
}
Run Code Online (Sandbox Code Playgroud)

这编译.当我从故事板添加属性时,我的问题出现了.该@IBOutlet标签产生一个编译器错误.

class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
    @IBOutlet weak var anotherThing: UILabel!  // error
    var thing: T!
}
Run Code Online (Sandbox Code Playgroud)

错误:

Variable in a generic class cannot be represented in Objective-C
Run Code Online (Sandbox Code Playgroud)

我实现这个权利吗?我该怎么做才能修复或解决这个错误?

编辑:

Swift 4终于有了解决这个问题的方法.看到我更新的答案.

generics protocols properties ios swift

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

Android,从文件计算SHA-1哈希,最快的算法

我在Android上遇到SHA-1性能问题.在C#中,我得到大约3秒的计算哈希,Android的相同计算大约需要75秒.我认为问题在于从文件中读取操作,但我不确定如何提高性能.

这是我的哈希生成方法.

private static String getSHA1FromFileContent(String filename)
    {

        try
        {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            //byte[] buffer = new byte[65536]; //created at start.
            InputStream fis = new FileInputStream(filename);
            int n = 0;
            while (n != -1)
            {
                n = fis.read(buffer);
                if (n > 0)
                {
                    digest.update(buffer, 0, n);
                }
            }
            byte[] digestResult = digest.digest();
            return asHex(digestResult);
        }
        catch (Exception e)
        {
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

任何想法如何才能提高性能?

hash performance android file sha

12
推荐指数
1
解决办法
5150
查看次数

Swift中基于字符串的枚举的下标字典

我想延长DictionaryString键(JSON字典),以便与任何下标enum,有一个RawValue类型的String.最终目标是多个enums可用于下标JSON词典的目标.

enum JSONKey: String {
    case one, two, three
}

enum OtherJSONKey: String {
    case a, b, c
}

if let one = jsonDictionary[.one] { /* ... */ }
if let b = jsonDictionary[.b] { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚如何实现这一点.我知道我需要扩展Dictionary,但无法弄清楚通用扩展约束或方法扩展约束.

我的第一个想法是尝试向下标方法添加通用约束.不过,我认为下标方法不允许使用泛型.

extension Dictionary {
    subscript<T: RawRepresentable>(key: T) -> Value? { /* ... */ }
}
Run Code Online (Sandbox Code Playgroud)

即使将泛型约束放在下标上,我仍然需要一种方法来嵌套我的泛型约束.或者将字典约束为基于字符串的枚举的键.要把它放在无效的代码中,我想这样做:

extension Dictionary where Key: RawRepresentable where RawValue == String { …
Run Code Online (Sandbox Code Playgroud)

generics enums dictionary swift swift-extensions

9
推荐指数
2
解决办法
2505
查看次数

反转范围会导致类型不匹配

我想使用一个变量来保存通常是一系列的东西,例如Range<Int>,这样我就可以使用条件逻辑来改变循环的范围,而无需复制/粘贴for循环.例如:

let range = aNumber % 2 == 0 ? 0..<10 : (0..<10).reverse()
for i in range { /* for loop logic */ }
Run Code Online (Sandbox Code Playgroud)

该行将let range = ...导致错误: Result values in '? :' expression have mismatching types 'Range<Int>' and 'ReverseRandomAccessCollection<Range(Int)'.我会猜测,反转范围会导致相同类型的范围或至少一个协议或两个值继承/实现的东西,所以我可以声明let range: SomeType = ....虽然我找不到.有任何想法吗?

for-loop iterable type-mismatch ios swift

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

pthread_join导致分段错误(简单程序)

我只是尝试使用多线程程序,但我遇到了pthread_join函数的问题.下面的代码只是一个我用来显示pthread_join崩溃的简单程序.此代码的输出将是:

before create

child thread

after create

Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

什么原因导致pthread_join给出分段错误?

#include <pthread.h>
#include <stdio.h>

void * dostuff() {
    printf("child thread\n");
    return NULL;
}

int main() {
    pthread_t p1;

    printf("before create\n");
    pthread_create(&p1, NULL, dostuff(), NULL);
    printf("after create\n");

    pthread_join(p1, NULL);
    printf("joined\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c multithreading pthreads segmentation-fault pthread-join

0
推荐指数
2
解决办法
5181
查看次数