小编Mak*_*lle的帖子

按两个属性排序

我的对象看起来像:

object = [[section: "section1", order: "1"],
          [section: "section2", order: "1"],
          [section: "section1", order: "2"],
          [section: "section2", order: "2"]]
Run Code Online (Sandbox Code Playgroud)

我想对它进行排序,得到如下结果:

[[section: "section1", order: "1"],
 [section: "section1", order: "2"],
 [section: "section2", order: "1"],
 [section: "section2", order: "2"]]
Run Code Online (Sandbox Code Playgroud)

所以我需要按部分排序,并在每个部分按顺序排序.

这就是我正在做的事情:

  return Observable.from(realm
            .objects(Section.self).sorted(byProperty: "order", ascending: true)
Run Code Online (Sandbox Code Playgroud)

字符串"section .."仅用于示例,它可以是其他的东西,所以我不能只使用字符进行排序.我需要X字符串的真正优先级.

ios swift swift3

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

从字符串中获取数字字符

如何从字符串中获取数字字符?我不想转换Int.

var string = "string_1"
var string2 = "string_20_certified"
Run Code Online (Sandbox Code Playgroud)

我的结果必须格式如下:

newString = "1"
newString2 = "20"
Run Code Online (Sandbox Code Playgroud)

swift swift3

4
推荐指数
2
解决办法
6993
查看次数

只能从 runloops 中添加通知块

我在这部分代码上有一些崩溃:

SRNetwork.provider
        .request(SRService.postData(user_id: userId))
        .mapArray(STrain.self)
        .observeOn(ConcurrentDispatchQueueScheduler.init(queue: SDispatchQueue.dataTrain.getQueue()))
        .subscribe({ (event) -> Void in
            switch event {
            case .next(let response):
                self.train.value = response
                SRealmTrain.sharedInstance.cacheTrain(response)
            case .error(let error):
                SRealmTrain.sharedInstance.fetchTrainRx(userId) //CRASH IS HERE
                    .bindTo(self.train)
                    .addDisposableTo(self.disposeBag)
                print("\(error)")
            default: break;
            }
        })
        .addDisposableTo(disposeBag);
Run Code Online (Sandbox Code Playgroud)

我认为问题在于我不在MainScheduler.instancerxRealm 上,main thread但我不想要它。有没有可能修复它?

fetchTrainRx

public func fetchTrainRx(_ userId: String) -> Observable<[STrain]> {

    let predicate = NSPredicate(format: "userId == %@", userId)

    if let realm = realm {

        return Observable.from(realm
            .objects(SRTrain.self)
            .filter(predicate)
            .sorted(byProperty: "order", ascending: true))
            .map ({
                $0.map(STrain.init)
        }) …
Run Code Online (Sandbox Code Playgroud)

realm swift rx-swift moya

4
推荐指数
1
解决办法
2615
查看次数

在地图上获取下一个值?

我正在尝试将元素与集合中的下一个元素进行比较.

例如 :

let array: [(Double, String)]= [(2.3, "ok"),
                                (1.4, "ok"),
                                (5.1, "notOk")]
Run Code Online (Sandbox Code Playgroud)

我需要一个返回的数组,它将汇总字符串相同的元素.所以我的结果将是:

new array = [(3.7, "ok"), (5.1, "notOk")]
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我需要做它的功能.我试图在地图中获取下一个元素,但无法找到.

像这样的东西(这只是为了逻辑,这段代码不起作用.

let newArray = array.map {(element, nextElement) in 
    if element.1 == nextElement.1 {
        return element.0 + nextElement.0 
    }
} 
Run Code Online (Sandbox Code Playgroud)

functional-programming swift

4
推荐指数
1
解决办法
1132
查看次数

无法使用Parse + Facebook身份验证

我试过使用parse + facebook auth.我正在关注一个教程,当我在文件上编译项目时,我得到了一些错误:PFFacebookAuthentificationProvider.h/m

/Users/.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:17:26: error: expected parameter declarator
@class BFTask PF_GENERIC(__covariant BFGenericType);
                     ^
/Users/Y.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:17:26: error: expected ')'
/Users/.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:17:25: note: to match this '('
@class BFTask PF_GENERIC(__covariant BFGenericType);
                    ^
/Users/.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:17:15: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
@class BFTask PF_GENERIC(__covariant BFGenericType);
          ^
/Users/.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:43:4: error: expected a type
 - (BFTask *)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *) *)readPermissions
^/Users/.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:43:51: error: nullability specifier 'nullable' cannot be applied to non-pointer type 'NSArray'
- (BFTask *)authenticateAsyncWithReadPermissions:(nullable NSArray PF_GENERIC(NSString *) *)readPermissions
                                              ^
/Users/.../Pods/ParseFacebookUtilsV4/ParseFacebookUtils/Internal/PFFacebookAuthenticationProvider.h:43:68: error: expected ')'
- …
Run Code Online (Sandbox Code Playgroud)

authentication facebook ios parse-platform

2
推荐指数
1
解决办法
326
查看次数

在 init 中设置属性并在 swift 中扩展?

我们如何使用extension来设置默认值?例如,如果我UILabel在我storyboardstoryboard. 现在我想创建一个扩展,以便在应用程序中初始化标签时将我的文本值设置为“popo”。更清楚地说,我想这样做是将整个应用程序中的默认值设置为我的 UILabel。希望这很清楚。

extension UILabel {
   open override func ??? { 
       self.text == "popo"
   }
}
Run Code Online (Sandbox Code Playgroud)

请不要告诉我在故事板中设置 ma 值,因为这不是我需要的。

uilabel ios swift

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

在 ScrollView 中按下时居中元素

我真的如何做一件可能很容易的事情..

我有一个带有一些按钮的 ScrollView。这就是我在滚动视图中创建所有按钮的方式。

var buttonList = [UIButton]()

func createButton() {
    let imageArray = fillImageArray()
    var lastButtonWidth: CGFloat = 0


    for index in 0..<6 {
        let frame1 = CGRect(x: ((self.view.frame.size.width / 2) - 27.5) + CGFloat(index * 70), y: 0, width: 55, height: 55 )
        let button = UIButton(frame: frame1)
        button.setImage(imageArray[index], forState: .Normal)
        button.tag = index
        button.addTarget(parentViewController, action: #selector(ViewController.buttonClicked(_:)), forControlEvents: .TouchUpInside)
        self.scrollView.addSubview(button)
        lastButtonWidth = frame1.origin.x
        buttonList.append(button)
    }
    self.scrollView.contentSize = CGSizeMake(lastButtonWidth + 55, 0)
}
Run Code Online (Sandbox Code Playgroud)

我希望当我按下我的一个按钮时,让他居中并正确定位其他按钮。

例子 : 在此处输入图片说明

如果我按 5 我想要这个结果:

在此处输入图片说明

按钮 …

iphone uiscrollview ios swift

0
推荐指数
1
解决办法
1163
查看次数

NSMutableAttributedString上的多个范围

我正在尝试string range在字符串中设置两个不同的粗体字体.我知道如何做到这一点rangerange,但有可能与多个范围内做一次.我不喜欢重复代码.

这是我的代码

if let rankString = trophy.fullRank.value {
    var secondRankString = "some string"
    var string = SRUtils.LocalizedString("unlocked.description", comment: "")
    string = String(format: string, trophy.trophiesNumber.value!, rankString)

    let atStr = NSMutableAttributedString(string: string)
    //Here i need to add rankString and other range
    let textRange = (string as NSString).range(of: rankString)

    if let font = UIFont(name: "HelveticaNeue-Bold", size: 16) {
        atStr.addAttribute(NSFontAttributeName, value: font, range: textRange)
        self.trophyDescription.value = atStr
    }
}
Run Code Online (Sandbox Code Playgroud)

如果例如:

var string = "Hello do you need …
Run Code Online (Sandbox Code Playgroud)

ios swift swift3

-1
推荐指数
1
解决办法
1324
查看次数