Swift - Use of unresolved identifier error after update - ios

I updated my OS to El Capitan and at the same time I guess XCode got updated (Now 7.2), prior to this when I would run my code in the simulator it worked fine. Now When I try to run the code for a calendar in my first view controller in a tabbed application it gives me the error "use of unresolved identifier 'SACalendar'" however everything else seems to be okay. I'm not sure if there was any syntax changes or something with the update, but it seems to be a common issue so I tried to review some other postings but was unable to find anything pertaining to my issue. Does anyone know whats going on? There error is in the code below, and SACalendar is a .m file that is included via bridging header. The bridging header does include all the necessary .h files, including the SACalendar.h
import UIKit
class FirstViewController: UIViewController, SACalendarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
/*
* Smooth scrolling in vertical direction
* - to change to horizontal, change the scrollDirection to ScrollDirectionHorizontal
* - to use paging scrolling, change pagingEnabled to true
* - the calendar works with any size
*/
//Get the width and the height of the device
let frameWidth = self.view!.frame.size.width
let frameHeight = self.view!.frame.size.height - 110
var calendar : SACalendar = SACalendar(frame: CGRectMake(0, 80, frameWidth, frameHeight), scrollDirection: ScrollDirectionVertical, pagingEnabled: true)
//error occurs due to the SACalendar on the righthand side of assignment operator
calendar.delegate = self
self.view!.addSubview(calendar)
}
}
The bridging header:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#ifndef YogaAdmin_Bridging_Header_h
#define YogaAdmin_Bridging_Header_h
#import "DateUtil.h"
#import "DMLazyScrollView.h"
#import "SACalendar.h"
#import "SACalendarCell.h"
#import "SACalendarConstants.h"
#endif

I was facing same issue and I was finding that delegate functions names same as class name which was creating issues in swift so i was solved it by change Delegate functions name.
-(void) SACalendar:(SACalendar*)calendar didDisplayCalendarForMonth:(int)month year:(int)year;
-(void) SACalendar:(SACalendar*)calendar didSelectDate:(int)day month:(int)month year:(int)year;
TO
-(void) Calendar:(SACalendar*)calendar didDisplayCalendarForMonth:(int)month year:(int)year;
-(void) Calendar:(SACalendar*)calendar didSelectDate:(int)day month:(int)month year:(int)year;
And Replaced SACalendar to Calendar where delegate functions are called.
Thanks

Related

Objective-C Can't Find Instance Method of Imported WKWebView

I am using a WKWebView and I am trying to set its magnification with the setMagnification instance method in a class that the webView is a variable of. Whenever I try to compile I get:
No visible #interface for 'WKWebView' declares the selector 'setMagnification:centeredAt:'
despite the fact that WebKit is being imported.
-(void)zoomIn:(int) xCoord y:(int) yCoord
{
CGPoint myPoint = CGPointMake(xCoord, yCoord);
[_webView setMagnification:3 centeredAt:myPoint]
}
Edit:
Just learned why this does not work is because it is OSX only, and I need it for iOS. If you have an alternative for iOS, please let me know.

How to get started using Siesta with Objective-C

I am inexperienced with Objective-C and this is my first attempt at anything Swift. I Can't get Xcode to compile. ( I may also be missing a necessary line or two, but if I get it to compile, I can usually figure out the rest )
I've been all over docs and getting started, I don't know enough to know the answer if it's there:
http://bustoutsolutions.github.io/siesta/guide/objc/
https://bustoutsolutions.github.io/siesta/api/
My Code:
// MyAPI.swift
import Siesta
import SwiftyJSON
#objc class MyAPI: Service {
internal static let instance = MyAPI(baseURL:"https://api.example.com")
}
// ViewController.mm
#import "ViewController.h"
#import "PlateScanner.h"
#import "Plate.h"
#import "MyProject-swift.h"
...
- (IBAction)callAPI:(id)sender
{
[[MyAPI.instance resource:#"/profile/"] child:#"123"];
}
-(void)resourceChanged: (BOSResource*) resource event: (NSString*) event {
if([event hasPrefix:#"NewData"]) {
NSLog(#"%#",[resource text]);
}
}
...
I get the following 2 errors from ViewController.mm:
1. line starting "[[MyAPI": No visible #interface for 'MyAPI' declares the selector 'resource:'
2. line starting "-(void)reso": Expected a type
From this I understand that my ViewController is not understanding the necessary BOSResource type, but I don't know how to get the swift library imported properly?
Xcode generated code:
//MyProject-swift.h
...
SWIFT_CLASS("_TtC10MyProject5MyAPI")
#interface MyAPI : BOSService
+ (MyAPI * _Nonnull)instance;
#end
...
With Error in line starting "#interf...": Cannot find interface declaration for 'BOSService', superclass of 'MyAPI'
Any help is greatly appreciated!

Objective-C protocol method invisible in Swift

I'm currently working on some Swift classes in my ObjC project.
The problem I have is the following:
I have this protocol declared in ClassA.h:
#protocol MyProtocol <NSObject>
- (void)complexMethodWithArg1:(id)arg1 arg2:(id)arg2 arg3:(id)arg3;
- (Folder *)currentDestinationFolder;
- (Flow)currentFlow;
#end
Pretty standard stuff.
Now my goal is to have a swift class with a property that is an object implementing this protocol.
So naturally, I add my class to the swift bridging header:
//
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "ClassA.h"
and declare my property in my swift file under ClassB which is a UIViewController that implement ANOTHER protocol
class ClassB : UIViewController, AnotherProtocol {
var delegate:MyProtocol?
}
Problem here is: I want to call a bunch of my delegate methods in viewDidLoad. It's working for all of them except ONE method that gets not autocompletion and errors the compilation if entered manually:
override func viewDidLoad() {
self.delegate?.currentDestinationFolder() // works great, no problem
self.delegate?.currentFlow() // works great, no problem
self.delegate?.complexMethodWithArg1(arg1: arg1, arg2: arg2, arg3: arg3) // PROBLEM : no autocompletion, error if entered manually !
super.viewDidLoad()
}
I have no idea what's going on, it's not related to optional or required protocol methods, not related to the fact that my delegate property is optional (tried unwrapped).
Has anybody face some similar issue? seems like some kind of bug?
I went ahead and tried to reproduce the problem on an empty project.
MyProtocol.h (taking the declaration from your question and comments)
#import Foundation;
#import UIKit;
#class CAPNavigationBar;
#protocol MyProtocol <NSObject>
- (void)setupNavigationItemInNavigationBar:(CAPNavigationBar *)navigationBar
navigationItem:(UINavigationItem *)navigationItem
inViewController:(UIViewController *)viewController;
#end
CAPNavigationBar.h (just a mock)
#import Foundation;
#interface CAPNavigationBar : NSObject
#end
ViewController.swift
import UIKit
class ViewController: UIViewController {
var delegate: MyProtocol?
override func viewDidLoad() {
super.viewDidLoad()
let capNavigationBar = CAPNavigationBar()
self.delegate?.setupNavigationItemInNavigationBar(capNavigationBar, navigationItem: nil, inViewController: self)
}
}
Bridging header
#import "MyProtocol.h"
#import "CAPNavigationBar.h"
Summary
Everything is working as expected.
You have either a simple typo somewhere or you are not importing correctly all the types into Swift. Especially make sure that you are not importing types only as forward declarations.

Cannot access variables in a Swift class from Objective-C file

I'm trying to continue with Swift in an existing project I had started in Objective-C. I followed the Apple documentation and I managed to access a Swift class from an Objective-C file, via including the "ProductModuleName-Swift.h" header in my Obj-C file.
At this point here's my code:
#include "Pianoconcert_App-Swift.h"
#interface ModelsVC ()
#end
#implementation ModelsVC
// And all that kind of stuff
// ...
-(IBAction)comanda:(UIButton *)sender {
ComandaTableVC *controller = (ComandaTableVC *)self.tabBarController.viewControllers[2];
// Here goes the problematic code
[self.tabBarController setSelectedIndex:2];
}
This piece of code has no problems. But now I just want to set one of the variables in the Swift class like this: controller.selectedModel = sender.tag, but Xcode just tells me the variable does not exist.
Here's an extract of my Swift class:
import UIKit
class ComandaTableVC: UITableViewController, UIPickerViewDataSource, UIPickerViewDelegate {
// Declaration of some constants and variables
// And here goes the one
var selectedModel: Int = 0
// And a bunch more of variables and functions
// ...
}
I don't know what I'm doing wrong. The class seems fully included and, actually, I can access that variable and all the others if I use the class from another Swift file, like this:
var controller: ComandaTableVC = self.tabBarController.viewControllers[2] as ComandaTableVC
controller.selectedModel = 2
It's been resolved finally, but I can't explain how.
Suddenly the compiler threw a build error telling me that the file Pianoconcert_App-Swift.h could not be found.
I changed it for PianoconcertApp-Swift.h, deleting the underscore that replaced the blank space, and now I can access all the variables and constants correctly.

How to use Objective-C code with #define macros in Swift

I'm trying to use a third-party Objective-C library in a Swift project of mine. I have the library successfully imported into Xcode, and I've made a <Project>-Bridging-Header.h file that's allowing me to use my Objective-C classes in Swift.
I seem to be running into one issue however: the Objective-C code includes a Constants.h file with the macro #define AD_SIZE CGSizeMake(320, 50). Importing Constants.h into my <Project>-Bridging-Header.h doesn't result in a global constant AD_SIZE that my Swift app can use.
I did some research and saw that the Apple documentation here under "Complex Macros" says that
“In Swift, you can use functions and generics to achieve the same
results [as complex macros] without any compromises. Therefore, the
complex macros that are in C and Objective-C source files are not made
available to your Swift code.”
After reading that, I got it to work fine by specifying let AD_SIZE = CGSizeMake(320, 50) in Swift, but I want to maintain future compatibility with the library in the event that these values change without me knowing.
Is there an easy fix for this in Swift or my bridging header? If not, is there a way to replace the #define AD_SIZE CGSizeMake(320, 50) in Constants.h and keep things backwards-compatible with any existing Objective-C apps that use the old AD_SIZE macro?
What I did is to create a class method that returns the #define.
Example:
.h file:
#define AD_SIZE CGSizeMake(320, 50)
+ (CGSize)adSize;
.m file:
+ (CGSize)adSize { return AD_SIZE; }
And in Swift:
Since this is a class method you can now use it almost as you would the #define.
If you change your #define macro - it will be reflected in the new method you created
In Swift:
let size = YourClass.adSize()
I resolved this by replacing
#define AD_SIZE CGSizeMake(320, 50)
in the library's Constants.h with
extern CGSize const AD_SIZE;
and adding
CGSize const AD_SIZE = { .width = 320.0f, .height = 50.0f };
in the library's Constants.m file.
write your constants after Class declaration. like this...
class ForgotPasswrdViewController: UIViewController {
let IS_IPHONE5 = fabs(UIScreen.mainScreen().bounds.size.height-568) < 1;
let Tag_iamTxtf = 101

Resources