Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
999 views
in Technique[技术] by (71.8m points)

swift - No such Module Error

I am working on a chat app using Swift, and cocoa pods and I keep getting this issue. It says no such module JSQMessagesViewController, and the pod file is set up just like the tutorial wants me to do it. If you need me to show you the pod file just let me know and I will put it in there.

Thanks Luc

Here is the code.

import UIKit
import JSQMessagesViewController
import syncano_ios

let syncanoChannelName = "messages"

class ViewController: JSQMessagesViewController {

let syncano = Syncano.sharedInstanceWithApiKey("7f5312e4808d69df6bd258b444b13d5d5956c15d", instanceName: "small-waterfall-9948")
let channel = SCChannel(name: syncanoChannelName)

let incomingBubble = JSQMessagesBubbleImageFactory().incomingMessagesBubbleImageWithColor(UIColor(red: 10/255, green: 180/255, blue: 230/255, alpha: 1.0))
let outgoingBubble = JSQMessagesBubbleImageFactory().outgoingMessagesBubbleImageWithColor(UIColor.lightGrayColor())
var messages = [JSQMessage]()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.setup()
    self.downloadNewestMessagesFromSyncano()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func reloadMessagesView() {
    self.collectionView?.reloadData()
}
}

//MARK - Setup
extension ViewController {
func addDemoMessages() {
    for i in 1...10 {
        let sender = (i%2 == 0) ? "Server" : self.senderId
        let messageContent = "Message nr. (i)"
        let message = JSQMessage(senderId: sender, displayName: sender, text: messageContent)
        self.messages += [message]
    }
    self.reloadMessagesView()
}

func setup() {
    self.senderId = UIDevice.currentDevice().identifierForVendor?.UUIDString
    self.senderDisplayName = UIDevice.currentDevice().identifierForVendor?.UUIDString
    self.channel.delegate = self
    self.channel.subscribeToChannel()
}
}

//MARK - Data Source
extension ViewController {

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.messages.count
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageData! {
    let data = self.messages[indexPath.row]
    return data
}

override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
    self.messages.removeAtIndex(indexPath.row)
}

override func collectionView(collectionView: JSQMessagesCollectionView!, messageBubbleImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageBubbleImageDataSource! {
    let data = messages[indexPath.row]
    switch(data.senderId) {
    case self.senderId:
        return self.outgoingBubble
    default:
        return self.incomingBubble
    }
}

override func collectionView(collectionView: JSQMessagesCollectionView!, avatarImageDataForItemAtIndexPath indexPath: NSIndexPath!) -> JSQMessageAvatarImageDataSource! {
    return nil
}
}

//MARK - Toolbar
extension ViewController {
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
    let message = JSQMessage(senderId: senderId, senderDisplayName: senderDisplayName, date: date, text: text)
    self.messages += [message]
    self.sendMessageToSyncano(message)
    self.finishSendingMessage()
}

override func didPressAccessoryButton(sender: UIButton!) {

}
}

//MARK - Syncano
extension ViewController {

func sendMessageToSyncano(message: JSQMessage) {
    let messageToSend = Message()
    messageToSend.text = message.text
    messageToSend.senderId = self.senderId
    messageToSend.channel = syncanoChannelName
    messageToSend.other_permissions = .Full
    messageToSend.saveWithCompletionBlock { error in
        if (error != nil) {
            //Super cool error handling
        }
    }
}

func downloadNewestMessagesFromSyncano() {
    Message.please().giveMeDataObjectsWithCompletion { objects, error in
        if let messages = objects as? [Message]! {
            self.messages = self.jsqMessagesFromSyncanoMessages(messages)
            self.finishReceivingMessage()
        }
    }
}

func jsqMessageFromSyncanoMessage(message: Message) -> JSQMessage {
    let jsqMessage = JSQMessage(senderId: message.senderId, senderDisplayName: message.senderId, date: message.created_at, text: message.text)
    return jsqMessage
}

func jsqMessagesFromSyncanoMessages(messages: [Message]) -> [JSQMessage] {
    var jsqMessages : [JSQMessage] = []
    for message in messages {
        jsqMessages.append(self.jsqMessageFromSyncanoMessage(message))
    }
    return jsqMessages
}
}

//MARK - Channels
extension ViewController : SCChannelDelegate {

func addMessageFromNotification(notification: SCChannelNotificationMessage) {
    let message = Message(fromDictionary: notification.payload)
    if message.senderId == self.senderId {
        //we don't need to add messages from ourselves
        return
    }
    self.messages.append(self.jsqMessageFromSyncanoMessage(message))
    self.finishReceivingMessage()
}

func updateMessageFromNotification(notification: SCChannelNotificationMessage) {

}

func deleteMessageFromNotification(notification: SCChannelNotificationMessage) {

}

func chanellDidReceivedNotificationMessage(notificationMessage: SCChannelNotificationMessage!) {
    switch(notificationMessage.action) {
    case .Create:
        self.addMessageFromNotification(notificationMessage)
    case .Delete:
        self.deleteMessageFromNotification(notificationMessage)
    case .Update:
        self.updateMessageFromNotification(notificationMessage)
    default:
        break
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Do the following things and you can import any swift file from "Pods"

1) Clean your project

2) Make sure that all your "Pods" > "Build Settings" > "Build Active Architecture Only" is set to "NO".

3) Don't run, just build your project.

4) Now, import any file from "Pods" to any swift file

E.g.: import JSQMessagesViewController

5) Again, build project and it will work as expected. Finally, you can access it properties

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...