小编sbk*_*bkl的帖子

Swift - 使用多个条件对对象数组进行排序

我有一个Contact对象数组:

var contacts:[Contact] = [Contact]()
Run Code Online (Sandbox Code Playgroud)

联系班级:

Class Contact:NSOBject {
    var firstName:String!
    var lastName:String!
}
Run Code Online (Sandbox Code Playgroud)

我希望按顺序对该数组进行排序,lastName然后firstName在某些联系人得到相同的情况下lastName.

我可以按照其中一个标准排序,但不能同时排序.

contacts.sortInPlace({$0.lastName < $1.lastName})
Run Code Online (Sandbox Code Playgroud)

如何添加更多条件来对此数组进行排序?

sorting swift

73
推荐指数
5
解决办法
3万
查看次数

react-native init(0.57)无法找到模块'@ babel/plugin-external-helpers'

当我创建一个全新的反应本机项目时,我收到此错误消息:

找不到模块'@ babel/plugin-external-helpers'

我的package.json文件:

{
  "name": "TestProject",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.5.0",
    "react-native": "0.57.0"
  },
  "devDependencies": {
    "babel-jest": "23.6.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.45.4",    
    "react-test-renderer": "16.5.0"
  },
  "jest": {
    "preset": "react-native"
  }
}
Run Code Online (Sandbox Code Playgroud)

知道什么是错的吗?

谢谢.

react-native

13
推荐指数
2
解决办法
2225
查看次数

UICollectionViewLayout无法按预期工作

我使用xcode 8和swift 3从raywenderlich.com 跟踪了这个Custom Collection View Layout教程.

当我在实现教程中请求的所有方法后运行应用程序时,我收到以下错误:

'-layoutAttributesForItemAtIndexPath没有UICollectionViewLayoutAttributes实例:{length = 2,path = 0 - 11}'

所以我在我的CollectionViewFlowLayout类中添加了以下方法:

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {

    return self.cache[indexPath.row]
}
Run Code Online (Sandbox Code Playgroud)

除了当向下滚动然后消失时某些单元格覆盖现有单元格时,这几乎正常工作.如果我向上滚动,一切都很完美.

我还不了解这段代码的完整逻辑,但我已经多次审查和测试过,我无法理解代码的哪一部分触发了这种行为.

任何的想法?

import UIKit

/* The heights are declared as constants outside of the class so they can be easily referenced elsewhere */
struct UltravisualLayoutConstants {
struct Cell {
    /* The height of the non-featured cell */
    static let standardHeight: CGFloat = 100
    /* The height of the first visible …
Run Code Online (Sandbox Code Playgroud)

uicollectionviewlayout swift3

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

Swift 3 - SwiftKeychainWrapper:KeychainWrapper.standard.set不是持久的

我在swift 3项目上安装了SwiftKeychainWrapper.

我使用以下方法来保存给定键的值:

let saveSuccessful: Bool = KeychainWrapper.standard.set("hello", forKey: "world")
Run Code Online (Sandbox Code Playgroud)

检查布尔变量的结果时,它为false.

当我尝试检索上面的键的值时,如下所示:

KeychainWrapper.standard.string(forKey: "world")
Run Code Online (Sandbox Code Playgroud)

结果当然是零.

知道我做错了什么吗?

xCode在此过程中根本检测不到任何错误.

谢谢您的帮助.

keychain swift3

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

Swift - JSQMessagesViewController - 单元格自定义

我从这个链接中了解到,您可以通过重写此方法来自定义JSQMessagesViewController库的单元格:

override func collectionView(collectionView: UICollectionView,  cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
 //Configure your cell here
return //yourCustomCell
}
Run Code Online (Sandbox Code Playgroud)

是否有可能获取示例代码以了解如何实现实际的自定义,包括JSQMessagesCollectionViewCell的子类?

例如,我想在消息气泡的底部添加一个标签,显示消息发送的时间.

谢谢.

编辑

我创建了一个自定义单元类如下:

class MyCustomCell: JSQMessagesCollectionViewCell {

    var textLabel: UILabel

    override init(frame: CGRect) {

        self.textLabel = UILabel(frame:  CGRect(x: 0, y: 0, width: frame.size.width, height: frame.size.height/3))
        super.init(frame: frame)

        self.textLabel.font = UIFont.systemFontOfSize(UIFont.smallSystemFontSize())
        self.textLabel.textAlignment = .Center
        contentView.addSubview(self.textLabel)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
Run Code Online (Sandbox Code Playgroud)

自定义单元格是以编程方式创建的,没有任何xib文件.

所以在我的JSQMessagesViewController中,我必须声明如下:

override func viewDidLoad() {

    super.viewDidLoad()

    self.collectionView!.registerClass(MyCustomCell.self, forCellWithReuseIdentifier: "MyCustomCell")
} …
Run Code Online (Sandbox Code Playgroud)

custom-cell swift jsqmessagesviewcontroller

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

Swift - 在iphone上将日期和时间对象从服务器转换为本地时间

我用php(laravel)向我的数据库发出请求,它返回一个日期对象,如下所示:

"lastMessageDate" : {
  "date" : "2016-06-06 23:37:32.000000",
  "timezone" : "UTC",
  "timezone_type" : 3
}
Run Code Online (Sandbox Code Playgroud)

如何在swift中将此对象转换为iPhone的本地日期和时间?

谢谢.

datetime-format swift

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

使用子域时 Laravel 护照 500 内部服务器错误

在我的本地机器和域http://myapp.test上使用 laravel 护照,我没有问题。

当我将代码推送到我的主域https://myapp.com上的服务器时,再次没问题。

但是,在推送到生产中的主域之前,我有一个 sudomain 用于我的实时测试(预生产)。如果我使用https://dev.myapp.com,则会收到 500 内部服务器错误。

知道如何修复它吗?

这是我正在做的狂饮电话:

$http = new GuzzleHttp\Client;

$response = $http->post('https://dev.myapp.com/oauth/token', [
    'form_params' => [
        'grant_type' => 'password',
        'client_id' => 'client_id',
        'client_secret' => 'client_secret_key',
        'username' => 'john@example.com',
        'password' => '123456',
        'scope' => '',
    ],
]);

return $response;
Run Code Online (Sandbox Code Playgroud)

如果我将 url 更改为我的本地或生产网站(更改护照密钥并确保存在相同的用户),它可以正常工作。

laravel laravel-passport

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