n00bie42 n00bie42 - 16 days ago 5
Swift Question

How do I expose functions I add to a file that I created with cocoa pods?

I'm sure this question has been answered but I have no idea how it would be worded so please bear with me, clearly I have a lot to learn about using libraries as this is my first time. I downloaded a library with pod install and am now trying to edit a class in it, but the function I add isn't exposed to my code outside of the pod file. Mind you, the pod came with a bunch of other files but I can't find any references to the other public variables of this class in them so I'm not sure how they are exposed to my outside code but the function I am creating (its the really obviously named one is not). Please help me

import UIKit

@objc public protocol WDImagePickerDelegate {
@objc optional func imagePicker(imagePicker: WDImagePicker, pickedImage: UIImage)
@objc optional func imagePickerDidCancel(imagePicker: WDImagePicker)
}

@objc public class WDImagePicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate, WDImageCropControllerDelegate {
public var delegate: WDImagePickerDelegate?
public var cropSize: CGSize!
public var resizableCropArea = false

public func myCustomFunctionAddedAfterPodInstall(){
print("hello world")
}

override public init() {
super.init()
print("WD was installed")
self.cropSize = CGSize(width: 320, height: 320)
_imagePickerController = UIImagePickerController()
_imagePickerController.delegate = self
_imagePickerController.sourceType = .photoLibrary
}

}

Answer

What I found is that XCode will not autocomplete that custom function, and it even gave a "no such function" error before I built and ran it. Once I did that, however, everything worked fine. Here's my code:

WDImagePicker.swift:

@objc open class WDImagePicker: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate, WDImageCropControllerDelegate {
    open var delegate: WDImagePickerDelegate?
    open var cropSize: CGSize!
    open var resizableCropArea = false

    public func myCustomFunctionAddedAfterPodInstall(){
        print("hello world")
    }

    ...
}

In my ViewController:

override func viewDidLoad() {
    super.viewDidLoad()


    imagePicker = WDImagePicker()
    imagePicker?.cropSize = CGSize(width: 280, height: 90)
    imagePicker?.delegate = self

    imagePicker?.myCustomFunctionAddedAfterPodInstall()

    self.present((imagePicker?.imagePickerController)!, animated: true, completion: nil)
}