小编MJQ*_*347的帖子

'#selector'的参数不是指'@objc'方法,属性或初始值设定项

我在Swift 3中提出了一个UIButton用Objective-C编写的子类.

当我尝试添加目标时,它失败并显示错误代码:

class ActionButton: JTImageButton {

    func action() {

    }

    func configure()) {
        // ...
        self.addTarget(self, action: #selector(self.action()), for: .touchUpInside)
        // error: 
        // Argument of '#selector' does not refer to an '@objc' method, property, or initializer

    }
}
Run Code Online (Sandbox Code Playgroud)

cocoa-touch objective-c ios swift

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

使用Firebase时使用未解析的标识符"FIRDatabase"

Xcode告诉我这FIRDatabase不是一个标识符.我的代码:

import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        FIRApp.configure()
        FIRDatabase.database().persistenceEnabled = true // Use of unresolved identifier 'FIRDatabase'
        return true
    }
}
Run Code Online (Sandbox Code Playgroud)

我在用:

Xcode 7.2.1,Firebase 3.0.2,OSX 10.10.5

Podfile内容:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target 'myapp' do
pod 'IQKeyboardManagerSwift', '<= 4.0.0'
pod 'JSQMessagesViewController', '<= 7.3.1'
pod 'Firebase'
end
Run Code Online (Sandbox Code Playgroud)

ios firebase swift

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

Firebase:如何在启用持久性时使用observeSingleEventOfType获取最新的服务器数据?

我非常喜欢使用Firebase进行编码.它是一个很棒的后端,有各种不同的工具集.但是,当启用持久性时,我想念一种检查更新数据路径的简单方法.我认为这不是一个很少的用例,因为我经常需要我的应用程序以某种方式操作,具体取决于最新的服务器数据,只需要读取一次.

我通常会使用observeSingleEventOfType,但是当启用perisistance时它没用,因为它永远不会检索最新的服务器数据.其中我不明白为什么.应该添加一个选项以跳过本地缓存并仅查找服务器数据.

禁用持久性可解决此问题,observeSingleEventOfType并将按预期工作.但这意味着需要自己重新实现所有的离线功能.

第一种情况:

 // chats contain meta information about the chat like last message and count of unread messages

 let chatRef = ref.child("chats").child(receiverId).child(chatId)
 chatRef.observeSingleEventOfType(.Value, withBlock: { (snapshot) -> Void in
     if !snapshot.exists() {
         print("snapshot does not exist")
         // the other side has deleted the chat
         // now delete all messages and member objects

         ref.child("messages").child(chatId).setValue(nil)
         ref.child("members").child(chatId).setValue(nil)
     } else {
         print("snapshot exists")
     }
 })
Run Code Online (Sandbox Code Playgroud)

chatRef.keepSynced(true)在观察没有运气的事件之前我也尝试过.无论如何,这在所有情况下都没有意义:

第二种情况:

func removeOlderMessages() {
    let dateInThePast = NSDate().addDays(-30).timeIntervalSince1970 * …
Run Code Online (Sandbox Code Playgroud)

ios firebase swift firebase-realtime-database

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

在Swift中将数组保存到Realm?

是否可以保存一组对象Realm?每当我对数组进行更改时,都应将其保存到Realm.

我目前的解决方案是使用a保存对象的对象for loop.对于追加/修改对象,调用save()将完成工作,但不是从我删除对象时.

class CustomObject: Object {
    dynamic var name = ""
    dynamic var id = 0

    override static func primaryKey() -> String? {
    return "id"
    }
}

struct RealmDatabase {

    static var sharedInstance = RealmDatabase()

    var realm: Realm!

    let object0 = CustomObject()
    let object1 = CustomObject()

    var array = [object0, object1]

    init() {
        self.realm = try! Realm()
    }

    func save() {

        for object in self.array {
            try! self.realm.write {
                self.realm.add(object, update: true)
            } …
Run Code Online (Sandbox Code Playgroud)

realm swift

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

如何改变iOS/Swift中的振动持续时间?

可以使设备振动AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)).但是还有一种方法可以设定振动过程的持续时间吗?我希望它更短.

我想在完全结束之前停止振动,例如在0.05秒之后.这可能吗?

iphone-vibrate ios swift

6
推荐指数
0
解决办法
6292
查看次数

Firebase/iOS:runTransactions有时不起作用

我正在开发一个聊天应用程序,用户应该收到有关其联系人新消息的通知.此通知消息还应包括未读消息的数量.因为发送方和接收方都可以更新此信息runTransaction是首选.不幸的是,有时它不起作用.感觉"卡住",然后再一次开始工作.该privateChats节点(见下文)被始终与最新的消息更新,而不是openChatMessages节点.

如果在很短的时间内发送了许多消息,即runTransactions对于同一个消息执行得太频繁,会发生这种情况ref吗?

我的数据结构:

privateChats
    $userId
        $chatId
            $messageId
                text
                timestamp
                senderId
                senderEmail
                senderName

// this node contains information about open chats
// like last message and counter for unread messages
openChatMessages
    $userId
        $chatId
            text
            timestamp
            senderId
            senderEmail
            senderName
            counter
Run Code Online (Sandbox Code Playgroud)

我的代码:

class ChatViewController: JSQMessagesViewController {

    var user: FIRUser!
    var ref: FIRDatabaseReference!
    var chatRef: FIRDatabaseReference!
    var senderOpenChatRef: FIRDatabaseReference!
    var receiverOpenChatRef: FIRDatabaseReference!

    // the following variables will be set before ChatViewController appears

    var chatId: …
Run Code Online (Sandbox Code Playgroud)

ios firebase swift firebase-realtime-database

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

如何检测最后一个单元格在UICollectionView中是否可见?

我试图检测a中的最后一个单元格collectionView是否可见.

var isLastCellVisible: Bool {

    let lastIndexPath = NSIndexPath(forItem: self.messages.count - 1, inSection: 0)
    let visibleIndexPaths = self.collectionView.indexPathsForVisibleItems()

    let lastCellPositionY = self.collectionView.layoutAttributesForItemAtIndexPath(lastIndexPath)!.frame.origin.y
    let bottomInset = self.collectionView.contentInset.bottom // changes when the keyboard is shown / hidden
    let contentHeight = self.collectionView.contentSize.height

    if visibleIndexPaths.contains(lastIndexPath) && (contentHeight - lastCellPositionY) > bottomInset {
        return true
    } else {
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)

什么有效:

如果最后一个单元格可见并且显示键盘以便单元格不被其隐藏,则上面的代码返回true.如果它是隐藏的,则返回false.

但是,当用户向上滚动并且最后一个单元格位于键盘上方时,我无法弄清楚如何返回true.

lastCellPositionY并且contentHeight当不改变collectionView被向上滚动.相反, self.collectionView.bounds.origin.y确实发生了变化,但我不知道如何与lastCellPositionY它进行比较,因为它们不是共享相同的起源而且self.collectionView.bounds.origin.y明显小于 …

cocoa-touch uicollectionview uicollectionviewlayout swift

6
推荐指数
2
解决办法
5884
查看次数

NoSQL / MongoDB 中的 1:1 和群聊模式

我想为 1:1 和群聊活动创建一个聊天应用程序。

我为这两种情况创建了一个架构:

{ // group 
    "id": 1 // id of the group
    "name": "Chat Group" // name of group; if there are more than 2 members
    "members": [ "member1", "member2", ...] // ids of the group chat members; only they have access to the JSON document
    "chatlog": [ "timestamp1": ["member1", "message1"], "timestamp2": ["member2", "message2"], ...] // who sent which message in chronological order
}
Run Code Online (Sandbox Code Playgroud)

如上所示,将用户的访问列表存储在“成员”数组中会"chat_groups"更好,还是这个解决方案更好:

{ // user 
    "id": 1 // id of the user
    "name": …
Run Code Online (Sandbox Code Playgroud)

mongodb nosql

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

如何正确刷新UINavigationBar?

关于这个问题:如何从子视图控制器中更改后退按钮文本?我正在寻找一种方法来更改后退按钮标题后刷新导航栏previousViewController.navigationItem.backBarButtonItem?.title = "New Title".

来自链接问题的(不太理想?)解决方案:

if let navigationController = self.navigationController {
    navigationController.popViewControllerAnimated(false)
    navigationController.pushViewController(self, animated: false)
}
Run Code Online (Sandbox Code Playgroud)

编辑:

显然,更改图层框会强制导航栏刷新.我认为不是解决方案,而是更便宜的(?)解决方法:

if let navigationController = self.navigationController {
    navigationController.navigationBar.layer.frame.insetInPlace(dx: 0.1, dy: 0)
    navigationController.navigationBar.layer.frame.insetInPlace(dx: -0.1, dy: 0)
}
Run Code Online (Sandbox Code Playgroud)

cocoa-touch ios swift

5
推荐指数
2
解决办法
6120
查看次数

更改 UIActivityViewController 中 UIBarButtonItems 的色调颜色

我有一个导航栏,在深色背景颜色上使用白色色调。我用来UIActivityViewController分享 iOS 分享表的链接。

当我选择 WhatsApp 或消息应用程序共享内容时,导航按钮具有默认的蓝色色调。此外,搜索栏(以 WhatsApp 为例)具有灰色色调,这确实使其难以阅读。

我无法更改色调颜色。我的代码:

let textToShare = "Visit my website!"
if let myWebsite = NSURL(string: "http://google.com") {
    let objectsToShare = [textToShare, myWebsite]
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

    self.presentViewController(activityVC, animated: true, completion: {
        activityVC.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
        activityVC.navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.whiteColor()   
        activityVC.navigationController?.navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
    })
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:

这不是重复的问题,因为它与活动视图控制器的按钮色调颜色无关。它与视图控制器的导航控制器的按钮颜色有关,例如按下 iMessage 上的共享后显示的按钮颜色。

ios uiactivityviewcontroller swift

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