It doesn't work AutoCompleteTextView with Xcode 7.3 swift 2

6 posts, 0 answers
  1. Cesar
    Cesar avatar
    1 posts
    Member since:
    Mar 2016

    Posted 22 Apr Link to this post

    Hello Everybody!

    I have tried to use the example of official documentation Telerik but it does not work, not whether it is compatible with Xcode 7.3 and swift 2, or I'm doing it wrong.

    The syntax has changed a bit in all controls, for example the alert if it worked.

    This is the code:

    ViewController class: UIViewController {
        let _dataSource = TKDataSource (array: sampleArrayOfStrings)
        let sampleArrayOfStrings = [ "Kristina Wolfe", "Freda Curtis", "Jeffery Francis", "Eva Lawson", "Emmett Santos", "Theresa Bryan," "Jenny Fuller", "Terrell Norris", "Eric Wheeler", " Julius Clayton "," Alfredo Thornton "," Roberto Romero, "" Orlando Mathis "," Edward Thomas "," Harry Douglas "]

        override func viewDidLoad () {
            super.viewDidLoad ()

            _dataSource.settings.autocomplete.createToken {(dataIndex, item) -> TKAutoCompleteToken? in
                let token = TKAutoCompleteToken (text: item)
                return token
            }

            self.automaticallyAdjustsScrollViewInsets = false
            let autocomplete = TKAutoCompleteTextView (frame: CGRect (x: 10, y: 85, width: self.exampleBounds.size.width - 20, height: 30))
            autocomplete.autoresizingMask = UIViewAutoresizing.FlexibleWidth
            autocomplete.dataSource = _dataSource
            self.view.addSubview (autocomplete)

            autocomplete.minimumCharactersToSearch = 1
            autocomplete.suggestionViewHeight = 150

            _dataSource.settings.autocomplete.completionMode = TKAutoCompleteCompletionMode.Contains
            autocomplete.suggestMode = TKAutoCompleteSuggestMode.SuggestAppend
        }
    }

    I hope you can help me

    Thank you!
  2. Sophi
    Admin
    Sophi avatar
    99 posts

    Posted 27 Apr Link to this post

    Hi Cesar,

    Thank you for your feedback.

    Indeed the current state of the documentation is out of date. This a known to us issue and in the next few weeks the documentation will be updated.
    If you need guidance you can use our sdk examples for reference, here is link to the repo.

    Regards,
    Sophi
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Robert
    Robert avatar
    7 posts
    Member since:
    Aug 2015

    Posted 01 May Link to this post

    Same here -- building a project based on the starter project in either Swift or XCode doesn't result in a workable program

    I'm referring to this documentation: http://docs.telerik.com/devtools/ios/AutoCompleteTextView/getting-started

    Objective-C -- I can follow the tutorial, and the program compiles, but when I run it, all I see is a blank white screen

    Swift -- The "Getting started" example created with the Swift tutorial won't even compile, throwing the following compile-time errors (the Swift tutorial obviously wasn't tested before being published!):

    1. "Instance member 'sampleArrayOfStrings' cannot be used on type ViewController" on the declaration line of _dataSource

    2. self.exampleBounds.size.width is used in the example, but this symbol doesn't exist

    I know the auto-complete *can* work when compiled with XCode 7.3 because I can compile and use the "telerikExamplesInSwift" project, and autocomplete works fine in that project, but the examplesinswift is so complex I can't really follow how to adapt what it does into a simple starter example, plus the swift example has Objective-C components (so it's not *really* a pure swift example anyway).

    Attached is the (non working) projects I created using the "getting started" documentation for this control in both Swift and Objective-C. Could someone at Telerik would create working versions of these examples and share them so we newbies to this control could more easily come up to speed how to leverage this control (especially in Swift applications)?

    Note: Nevermind on the attachment.  Your system said, "The selected file(s) cannot be attached because it may exceed the maximum attachment size (2 MB) or is from not allowed type (allowed: .jpg, .jpeg, .gif, .png).".  But please try the getting started in XCOde 7.3 and post a working version of the resulting tutorial application somewhere...really want to use this control!

  5. Miroslava
    Admin
    Miroslava avatar
    19 posts

    Posted 04 May Link to this post

    Hello Robert,

    I have modified and attached your project in the other thread that you created. 


    Regards,
    Miroslava
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  6. Armando
    Armando avatar
    4 posts
    Member since:
    Nov 2015

    Posted 11 May Link to this post

    Did you get it to work? i'm having the same problem, i can't get AutoCompleteTextView to work with xcode 7.3 and swift 2.2

    i'm following this example:

    https://github.com/telerik/ios-sdk/blob/master/TelerikUIExamplesInSwift/TelerikUIExamplesInSwift/AutoCompleteLoadData.swift

    but all i get is a blank screen without the AutoComplete

  7. Miroslava
    Admin
    Miroslava avatar
    19 posts

    Posted 13 May Link to this post

    Hello Armando,

    The examples from this repository are out of date. We will update them in the next weeks.
    Consider the code snippet below:
    class AutoCompleteLoadData: TKAutoCompleteController, TKAutoCompleteDataSource
    {
        var airports = NSArray()
         
        override func viewDidLoad()
        {
            NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AutoCompleteLoadData.keyboardDidShow(_:)), name: UIKeyboardDidShowNotification, object: nil);
     
            autocomplete = TKAutoCompleteTextView(frame:CGRect(x: 10, y: 20, width: self.view.bounds.size.width-20, height: 44))
            self.automaticallyAdjustsScrollViewInsets = false
     
            autocomplete.autoresizingMask = UIViewAutoresizing.FlexibleWidth
            self.view.backgroundColor = UIColor(red: 0.937, green: 0.937, blue: 0.957, alpha: 1.0)
     
            self.autocomplete.suggestMode = TKAutoCompleteSuggestMode.Suggest
            autocomplete.dataSource = self
            let btnImage = UIImage(named: "clear")
            autocomplete.closeButton.setImage(btnImage, forState: UIControlState.Normal)
            autocomplete.imageView.image = UIImage(named: "search")
             autocomplete.minimumCharactersToSearch = 1
            autocomplete.suggestionViewHeight = self.view.bounds.size.height - self.view.bounds.origin.y + 45;
            
        }
         
        func autoComplete(autocomplete: TKAutoCompleteTextView, completionsForString input: String)
        {
            let suggestions = NSMutableArray()
            let priority = DISPATCH_QUEUE_PRIORITY_HIGH
             
            dispatch_async(dispatch_get_global_queue(priority, 0)) {
                if(self.airports.count == 0) {
                let url = NSURL(string: self.urlStr)
                let req = NSURLRequest(URL: url!)
                let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
                var dataVal :NSData = NSData()
                var jsonResult:NSDictionary = NSDictionary()
                do {
                    dataVal =  try NSURLConnection.sendSynchronousRequest(req, returningResponse: response)
                }
                catch {
                    print(error)
                }
                 
                do {
                    jsonResult = try NSJSONSerialization.JSONObjectWithData(dataVal, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
                }
                catch {
                    print(error)
                }
                     
                self.airports = jsonResult.objectForKey("airports") as! NSArray
            }
     
                for airport in self.airports {
                     
                    let dict = airport as! NSDictionary
                    let name = dict.objectForKey("FIELD2") as! NSString
                    let shortName = dict.objectForKey("FIELD5") as! NSString
                    let current = NSString(format: "%@, %@",name, shortName)
                     
                    if(current.uppercaseString.hasPrefix(input.uppercaseString)){
                        suggestions.addObject(TKAutoCompleteToken(text: current as String))
                    }
                }
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0), dispatch_get_main_queue(), { () -> Void in
                    autocomplete.completeSuggestionViewPopulation(suggestions as [AnyObject])
                })
            }
        }
         
        func keyboardDidShow(notification: NSNotification)
        {
          let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()
          autocomplete.suggestionViewHeight = self.view.bounds.size.height -  keyboardSize!.height - 100
        }
    }


    Regards,
    Miroslava
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
DevCraft banner