小编dri*_*ing的帖子

如何将AnyClass转换为特定的类并在Swift中动态初始化它?

Object-C中,我将Class对象存储在一个数组中并像这样动态地初始化它们:

self.controllers=@[[OneViewController class],[TwoViewController class]];
Class clz = self.controllers[index];
UIViewController *detailViewController = [[clz alloc] init];
Run Code Online (Sandbox Code Playgroud)

Swift中我尝试这种方式,但它引发了一个错误:

var controllers:AnyClass[] = [OneViewController.self,TwoViewController.self]
var clz : AnyClass = controllers[index]
var instance = clz() // Error:'AnyObject' is not constructible with ()
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法将AnyClass转换为特定的类?还是其他任何好主意?

swift

50
推荐指数
4
解决办法
3万
查看次数

如何在Realm中设置自动增量键?

我为每个ChatData对象都有一个唯一的msgid.

@interface ChatData : RLMObject
@property NSInteger msgid;
....
@end
Run Code Online (Sandbox Code Playgroud)

但每次我创建一个新对象时,我必须查询所有对象并获取最后一个msgid.

RLMArray *all = [[ChatData allObjects] arraySortedByProperty:@"msgid" ascending:YES];
ChatData *last = [all lastObject];
ChatData *newData = [[ChataData alloc]init];
newData.msgid = last.msgid+1;
Run Code Online (Sandbox Code Playgroud)

有没有一种有效的方法来替换这种实现?

realm ios

34
推荐指数
4
解决办法
3万
查看次数

我如何处理Int和CGFloat之间的操作?

在Objective-C中,我可以执行以下操作:

Objective-C代码:

CGFloat width = CGRectGetWidth(mView.bounds);
CGFloat total = width*[myArray count];
Run Code Online (Sandbox Code Playgroud)

但在Swift中,它会引发一个错误:

找不到接受提供的参数的'*'的重载

我怎样才能优雅地避免这种情况?

objective-c swift

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

iOS为什么设置NSBaselineOffsetAttributeName时NSTextAttachment会消失?

之前

我想将NSTextAttachment与文本的中心对齐,因此我设置NSBaselineOffsetAttributeName来更改NSTextAttachment的基线.

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"STHeitiSC-Light" size:17]}];
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"micon"];        
NSMutableAttributedString *ats = [[NSAttributedString attributedStringWithAttachment:attachment] mutableCopy];
[ats addAttributes:@{NSBaselineOffsetAttributeName:@(-5),NSFontAttributeName: [UIFont fontWithName:@"STHeitiSC-Light" size:17]} range:(NSRange){0,ats.length}];
[attrString appendAttributedString:s];
Run Code Online (Sandbox Code Playgroud)

然后我计算UILabel的大小并设置attributedText.

CGRect textFrame = [attrString boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
UILabel *label = [[UILabel alloc] initWithFrame:textFrame];
label.lineBreakMode = NSLineBreakByCharWrapping;
label.numberOfLines = 0;
label.attributedText = attributed;
label.backgroundColor = [UIColor clearColor];
Run Code Online (Sandbox Code Playgroud)

最后,最后一张图片消失了.

后

任何人都可以解释为什么会发生这种情况以及如何解决它.

iphone ios

9
推荐指数
1
解决办法
2429
查看次数

ReactiveCocoa和PromiseKit之间的主要区别是什么?

ReactiveCocoa使用RACSignal和PromiseKit使用Promise来包装值.它们可以将异步工作链接在一起.ReactiveCocoa和PromiseKit之间的主要设计区别是什么?

promise ios reactive-cocoa

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

如果设置内部元素位置,绝对不能通过filter(":visible")找到外部元素的原因?

我想隐藏元素,如果它被过滤器检测到("可见").

我们可以看到内部图片是可见的,但是<a>如果设置<img>position:absolute ,为什么无法找到外部元素?

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <style type="text/css">
        a {
            position: absolute;
        }
        img {
            position: absolute; //the filter() works if i annotate this line
        }
    </style>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("#container a").filter(":visible").hide()
        });
    </script>
</head>
<body>
<div id="container">
    <a href=""><img src="http://sudasuta.com/script/timthumb.php?src=http://sudasuta.com/wp-content/uploads/2014/03/he-British-Library-2.jpg&h=200&w=260&zc=1"></a>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

html javascript css jquery

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

如何获取 Xcode Playground 的当前工作目录?

我正在使用基于 OSX 的 Playground。当我尝试时,NSFileManager.defaultManager().currentDirectoryPath它返回/这不是我的工作目录。

macos swift-playground

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