Using iosMath in Swift - ios

how can I use iosMath in Swift on iOS, because iosMath is written in Objective C? Or is there another way to display mathematical formulas with Swift?

I will make this as fast as possible:
1.) install cocoaPods (open up terminal and write sudo gem install cocoapods)
2.) From terminal change directory to your project and init cocoaPods and edit the Podfile like this:
cd /ProjectFolder
pod init
nano podFile
(opens the podfile, where you paste the pod repo) , should look like this:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'mathExample' do
# Comment the next line if you're not using Swift and don't want to use dynam$
use_frameworks!
# Pods for mathExample
pod 'iosMath'
end
save podfile and install via:
pod install
from now, use the workspace file instead of the project file... We are done installing the pod you need right now...
Now it gets a little bit tricky -> You need to add new Objective-C file into the project (so just New File -> Objective-C file and select create bridging header) from now, you can just delete the created file (BUT NOT THE BRIDGING HEADER)
from now, you can use the Objective-C framework just as it was swift framework,don't forget to import that framework into classes you will use it:
import UIKit
//Importing our module
import iosMath
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print(iosMathVersionNumber)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Hope this helps, wish happy coding! :)

The general question has already been answered. Check this question out.
All you have to do is add iosMath in your podfile and as long as you have use_frameworks! in the podfile, you won't need any headers. Simply, import iosMath in your swift file and you are good to go.

Related

CocoaPods Swift Unresolved Identifier

I am trying to create my own CocoaPod. I have updated cocoapods to the latest version (sudo gem install cocoapods) and then ran this: pod lib create MySwiftLibrary. I set the language to Swift, used the Quick testing framework, added an auto-generated example project, and did not include UI tests. I then navigated to MySwiftLibrary/Example and ran pod install.
That way, my example project was configured. I opened the *.xcworkspace in the auto-generated example project, navigated to Pods -> Development Pods/MySwiftLibrary/MySwiftLibrary/Classes/ReplaceMe.swift and added the following code:
public class ReplaceMe : UILabel {
public func testMe() -> String {
return "Hello World"
}
}
I ran pod install again, and in the example part of the project (MySwiftLibrary -> Example for MySwiftLibrary/ViewController.swift) I added the line import MySwiftLibrary and modified the viewDidLoad method such that the file looked as follows:
import UIKit
import MySwiftLibrary
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = ReplaceMe()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I ran pod install and pod update, just to be sure, but when trying to build the project, I get the following error: Use of unresolved identifier 'ReplaceMe'. What am I doing wrong? This is literally the barest of bare-bone project modifications.
UPDATE: I just found out the issue, and it is something a responded could not have guessed. I have anonymized the name of the pod. In reality, I didn't call the pod MySwiftLibrary, but MyLibrarySwift. (MyLibrary is still not what it really was). I tried making that same pod again and again, and it didn't work. But then I had the idea to try a name that did not end with Swift, and it instantly started working.

Failed to recognize AWSDynamoDB with XCode 7.0 Beta, iOS 8.3, and aws-ios-sdk-2.2.0

I am matching guides for using AWSDynamoDB in a test project using Swift 2 in XCode 7.0 Beta. I am required to use that platform instead of the previous stable one and make it work.
I am using the next links:
https://docs.aws.amazon.com/mobile/sdkforios/developerguide/setup.html
http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LoadData_Java.html
Best way to make Amazon AWS DynamoDB queries using Swift?
I already made it work for reading and writing text files and images to an S3 Bucket, but now that I am trying to use DynamoDB service something might be missing.
Here is my Podfile contents:
# Uncomment this line to define a global platform for your project
platform :ios, '8.3'
target 'AWSSDKTest' do
source 'https://github.com/CocoaPods/Specs.git'
pod 'AWSCore'
pod 'AWSAutoScaling'
pod 'AWSCloudWatch'
pod 'AWSDynamoDB'
pod 'AWSEC2'
pod 'AWSElasticLoadBalancing'
pod 'AWSKinesis'
pod 'AWSLambda'
pod 'AWSMachineLearning'
pod 'AWSMobileAnalytics'
pod 'AWSS3'
pod 'AWSSES'
pod 'AWSSimpleDB'
pod 'AWSSNS'
pod 'AWSSQS'
pod 'AWSCognito'
end
target 'AWSSDKTestTests' do
end
I had to delete Podfile.lock and Pods folder, deleted Configuration Sets in Project->Info->Configurations, and installed pods again.
Here is my bridging.h file:
#ifndef AWSSDKTest_bridging_h
#define AWSSDKTest_bridging_h
#endif
#import <AWSCore/AWSCore.h>
#import <AWSS3/AWSS3.h>
#import <AWSDynamoDB/AWSDynamoDB.h>
#import <AWSSQS/AWSSQS.h>
#import <AWSSNS/AWSSNS.h>
#import <AWSCognito/AWSCognito.h>
I downloaded the aws-ios-sdk-2.2.0.zip file, unzipped and added to Frameworks all the AWS frameworks.
I am trying to use AWSDynamoDBModel, here is a swift file for implementing the Upload Sample Items Example for using DynamoDB Mapper:
import Foundation
class Forum : AWSDynamoDBModel, AWSDynamoDBModeling {
var name : String = ""
var category : String = ""
var threads : Int = 0
var messages : Int = 0
var views : Int = 0
// override init!() { super.init() }
required init!(coder: NSCoder!) {
fatalError("init(coder:) has not been implemented")
}
class func dynamoDBTableName() -> String! {
return "Demo"
}
class func hashKeyAttribute() -> String! {
return "email"
}
class func rangeKeyAttribute() -> String! {
return "date"
}
/*
override init(dictionary dictionaryValue: [NSObject : AnyObject]!, error: NSErrorPointer) {
super.init(dictionary: dictionaryValue, error: error)
}
override func isEqual(anObject: AnyObject?) -> Bool {
return super.isEqual(anObject)
}
*/
}
I had to comment the lines that caused error because those might be fixed. The errors mentioned that those functions couldnt be overridden and that super.init couldnt be called inside a root method.
After cleaning and building again, the error is at the class declaration line
class Forum : AWSDynamoDBModel, AWSDynamoDBModeling {
The error says: Use of undeclared type 'AWSDynamoDBModel'
If I try writing other AWSDynamoDB classes they don't appear in the list of suggestions and then cause the same error.
Additionally I want to mention that in the developer's guide setup (first link above) the 4th point of the Getting Started with Swift section says: "Import the AWSCore header in the application delegate":
#import <AWSCore/AWSCore.h>
Than can't be done, only in the bridging.h file which is mentioned in the 2nd point.
My first request of assistance is in fixing the error mentioned and making the project recognize the AWSDynamoDB framework.
Then I request your help for any observation about this merge of different tutorials, also any other online tutorial or guide that is more clear than those mentioned.
In case you are using CocoaPods (0.36) with "use_frameworks!", this answer might solve your problem:
"Normally when you’re importing Objective-C code into Swift, you do so by including the header of the file containing that code in the “Bridging Header” for your project. And that is indeed how you include code from a static library (which your pods used to be.)
But it is not how your import Objective-C code from a Framework. To do that you simply type…
import Framework
…inside your Swift file that’s using the Objective-C class (where “Framework” is the name of the actual Framework containing the class.)"
Source here: http://rogueleaderr.com/post/115372471213/unresolved-identifier-in-swift-when-importing
I think the problem is not related to Xcode7Beta, but the installation process of your project. (My sample DynamoDB project runs fine under Xcode7Beta.)
If you installed the AWS Mobile SDK via cocoapods, you neither have to worry about the bridging file nor need to download aws-ios-sdk-2.2.0.zip file since cocoapods already did everything for you.
My suggestion would be:
clean your project, remove all aws related frameworks, bridging files that you manually added,delete Podfile.lock and Pods folder and AWSSDKTest.xcworkspace file, and the re integrate pods by running "pod install"
Open your project using Xcode 6.4, confirm it can be built and run successfully under Xcode 6.
If everything looks good via Xcode 6, reopen it via Xcode7Beta, If it failed to compile, please post the error output so I can take a look.
Thanks

PureLayout in Swift with Cocoapods

Using cocoapods-0.37.2 Here's my Podfile:
platform :ios, "8.0"
use_frameworks!
pod "GoogleMaps"
pod 'FontAwesomeKit'
pod 'PureLayout'
I'm trying to import PureLayout in my .swift file like this:
import PureLayout
class ViewController: UIViewController {
...
But I get error:
No such module 'PureLayout'
Why??? I thought that Cocoapods creates -Bridging-Header.h by itself when using use_frameworks! ?
I did not use the use_framework option but I used PureLayout in Swift with CocoaPods without issues.
My Podfile only has one line pod 'PureLayout'.
I created the <Your Module>-Bridging-Header.h file myself as instructed by Apple here. I added a line #import "PureLayout.h". I've also updated my target build settings to enter the bridging header file name as per instructions.
In the .swift file where I want to use PureLayout, I did not need to have import PureLayout. It would give me an error as you mentioned. The auto layout calls would just work, since it's defined through bridging header.
One side point - the test target would fail to compile if you add bridging header file to its build settings. So make sure the bridging header update is done on target not project level.
I guess the current README in PureLayout github page is a bit outdated, since it currently asks you to do import PureLayout in the .swift file. I added a comment in this README enhancement issue too.
install the podfile
open your .xcworkspace
put your import PureLayout statement in, get the error
delete the statement
clean your product
build your product
put your import PureLayout statement back in
This has been common occurence and fix for me when using outside frameworks.
spacemonkey's method work for me. In that case, you don't even need a -Bridging-Header.h file.
As for FontAwesomeKit, since it contains some resource file(xx.ttf, xx.otf, etc), you should add this file to project to make it work. Like following shows:
Clean your project and recompile, then you are done.
Following is my demo for this:
import FontAwesomeKit
import ChameleonFramework
import SnapKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let iconSize : CGFloat = 128;
let codeIcon = FAKFontAwesome.codepenIconWithSize(iconSize);
imageView.image = codeIcon.imageWithSize(CGSizeMake(iconSize, iconSize));
view.backgroundColor = UIColor.flatGreenColorDark();
}
}
PS. Since you use swift, maybe the SnapKit Layout framework will much suitable than PureLayout.

Mixed CocoaPods project: ObjC functions are visible from Swift, class variables are not

I have set up a Swift iOS project with CocoaPods. I want to use pods based on both Swift and Objective C. Let's say I want to use KeychainAccess (Swift-based) and CocoaLumberjack (Objective C-based). Here's the Podfile I use:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'KeychainAccess'
pod 'CocoaLumberjack'
Now, I try to use these pods:
import UIKit
import KeychainAccess
import CocoaLumberjack
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// this works fine
let keychain = Keychain(service: "access-token")
// this works as well
DDLog.addLogger(DDTTYLogger.sharedInstance())
// 'DDLog.Type' does not have a member named 'logLevel'
DDLog.logLevel = .Verbose
// 'DDLog.Type' does not have a member named 'logAsync'
DDLog.logAsync = false
}
}
Am I missing something? How do I make CocoaLumberjack work? (or any other ObjC pod, I assume I will have the same issue with other pods as well).
I have created a project which reproduces this issue.

No autocomplete on cocapods for AppCode 3.1

I have recently set up a project from a base XCode Project with the cocoapods installation, and when importing the project into AppCode 3.1, I am not getting any autocompletion for my swift frameworks that are installed through CocoaPods
Here is my Podfile
# Uncomment this line to define a global platform for your project
platform :osx, '10.10'
use_frameworks!
target 'Main' do
pod "SwiftyJSON", ">= 2.1.3"
pod 'BrightFutures', '~> 1.0.0-beta.3'
end
target 'MainTests' do
end
As you can see, I have 2 pods defined (one is SwiftyJson and one is BrightFutures). Both of these pods are installed as Swift Frameworks (hence why use_frameworks!) is there.
Everything appears to be imported and working correctly for AppCode (I have two projects, one is the main project, and the other one is Pods, which is the exact same as it is in XCode), however it appears that AppCode doesn't index any of the Pod frameworks, so I am not getting autocompletion status on using those frameworks, at all. As an example, in the following code
import Cocoa
import BrightFutures
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
let f = future { 5 }.map{f in f + 7}
f.onSuccess{callback in println(callback)}
// Insert code here to initialize your application
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}
I get no autocompletion on the following
import BrightFutures (importing anything else that isn't from CocoaPods works fine)
Auto complete on anything on this line let f = future { 5 }.map{f in f + 7} or this one f.onSuccess{callback in println(callback)}
Autocomplete works fine on anything else (i.e. aNotification) which is an argument on the applicationDidFinishLaunching function
Is there some setting in AppCode that I am missing, or is this something that hasn't been implemented yet? In XCode, autocompletion works completely correctly.
EDIT: I have also noticed that in AppCode, the .framework files are in red (not sure if this means anything?)

Resources