How do I use Realm in a Swift Playground? - ios

So, I'm pretty new to XCode development, but I've read the documentation on how to import third party frameworks into a Playground in XCode 7.1.1, and the documentation on how to get started with Realm, using the Dynamic Framework method of installation.
At this point I have a workspace that contains an iOS project, the two .framework folders required by Realm, and a Playground project. I have built the workspace without errors.
I figured going through some of the Realm examples in the Playground would be a great way to get going, but doing a simple:
import RealmSwift
results in a "No such module 'RealmSwift' error. What am I missing?

You should install Realm with Cocoapods.
One good practice to adopt is using Cocoapods when using other librairies like this one when they support it. That way you always have control on the version you're using of this library.

Update: Realm Swift works with Swift Playgrounds now. You'll find an example in Realm's release zips (direct download).
Realm doesn't support Swift Playgrounds at the moment: https://github.com/realm/realm-cocoa/issues/938

I faced the same issue while playing with RealmSwift on a Playground. I create it under a single view iOS application. Fixed it after building my main target, as creates the dynamic framework to be linked into the Playground.
Working fine with Cocoapod installation, Swift 4.2, & Xcode 10.1
Try it yourself. https://github.com/sauvikdolui/RealmExploration

Related

PayMill SDK iOS swift 3.0 Xcode 8.1

I'm trying to use PayMill SDK in a Swift project.
I used CocoaPods as described by PayMill, the framework, and it generated 'Pods_PayMilDSK'
After that I tried to import it on my file but I can't get access to any to the functions included in the framework.
I also tried to import the framework directly in my project but I can't access the PayMillSDK framework (i.e. import does not 'see' it).
Any help would be greatly appreciated.
Rodolphe
After running pod install You should always Build your project, cmd + B, because your project still hasn't read the contents of the library you just installed.
Also make sure you're using the .xcworkspace and not the .xcodeproj
I can now make the import of the PAyMill module but functions are not available

Xcode can't read swift files

This project came to me to be updated to the new iOS 10.It uses frameworks downloaded with cocoa pods. Some of these frameworks are written in swift.Now that I made few changes on the project code, when I try to build the project, I get errors on the frameworks I downloaded as if the project can't read swift!
Is there somewhere where I can set my project to accept swift classes?
Updated Xcode to Xcode 8 and it updated the code and fixes theses issues. Thanks Folks.

Issue while creating dynamic framework in Swift with carthage

Hi Can any one help in resolving this issue.
I have created a dynamic framework in swift. My framework is referring some external frameworks like alamofire, xcglogger etc into my framework project. I am using carthage for this purpose.
It works perfectly. But when I create the framework and use it in another project its giving me error on my framework
Missing required modules : 'Alamofire','XCGlogger'
Can anyone help me in this.
I am using carthage 0.16.2
xcode 7.3.1
Swift

Realm: Module was created by an older version of compiler and could not build objective-c module

I have followed the steps provided to add the framewowrk from this link https://realm.io/docs/swift/latest/
It gives this error what could be the issue?
Is it because it is compiled with an older version of xcode, since I have the latest xcode 7 beta?
If so, how would I solve this?
You'll have to wait for us to support Swift 2.0 (#2062) before you can use Realm Swift in your Swift 2.0 project.
Because the framework written in Swift which was built in the old version compiler is not able to use in Xcode 7. Therefore, it will be needed to re-build using the compiler of Xcode 7. However, Swift 2 is required on Xcode 7; we are working to adapt RealmSwift.framework to the Swift 2.
I got Realm to work with Swift 2 by doing the following (this is from memory)
Checkout Swift-2.0 branch https://github.com/realm/realm-cocoa/tree/swift-2.0
Build by running /build.sh ios-swift
Copy Realm.framework and RealmSwift.framework into project
Use let realm = try! Realm() as my initializer in place of let realm = Realm()
I use Carthage for all my frameworks, so to keep things uniform I copied the RealmSwift and Realm frameworks into the carthage/build/ios directory and the /usr/local/bin/carthage copy-frameworks build script picks them up
This just randomly started happening to me as well for some reason. Manually compiling the swift-2.0 branch didn't end up working at all.
I ended up converting my entire project to Swift 2.1, and using the pre compiled frameworks. Now I'm up and running.
FWIW - The conversion to 2.1 from 2.0 wasn't nearly as painful as any of the other conversions from 1.0, or 1.2.

How to I import 3rd party frameworks into Xcode Playground?

How do I import 3rd part frameworks into Xcode Playground?
Swift Playground obviously has a framework import mechanism because we can import Cocoa, SpriteKit, and in an OSX Playground, XCPlayground (XCPlayground seems missing from iOS, oddly)
What I'd really like to do is hide code from my playground and be able to show a minimal example. Any thoughts on how to load a Framework into Swift playground?
See also:
How to import own classes from your own project into a Playground
Is it possible to import third-party Objective-C code into a Swift playground?
(This question is different because the request is to use a framework in Playground rather than simply regular swift files)
There is currently no supported way to import your own framework or app code into a playground, short of pasting it into the playground editor. We're aware that this is very desirable functionality, but as always we encourage people to "vote with bugreporter" at bugreport.apple.com
Right now, if you're just trying to hide code for showing in an example, code folding in the editor might do the trick.
It is now possible to import your own frameworks into a playground. This provides a way to share code between your applications and playgrounds, which can both import your frameworks. To do this, your playground must be in the same workspace as the project that produces your framework. You must have already built your framework. If it is an iOS framework, it must be built for a 64-bit run destination (e.g. iPhone 5s). You must have an active scheme which builds at least one target (that target's build location will be used in the framework search path for the playground). Your "Build Location" preference (in advanced "Locations" settings) should not be set to "Legacy". If your framework is not a Swift framework the "Defines Module" build setting must be set to "Yes". Once all these conditions are fulfilled, importing your framework will work in a playground.
Edited: As of Beta5 this is now supported when the playground is part of the workspace that builds the framework as a target. There are more details on the Apple dev forums site, but hopefully #Rick Ballard will add them to his answer here and that should becoke the definitive answer for this question.
Don't do the below anymore!
For the short term, while there's no supported solution, if you're producing a Module/Framework you can copy it into the SDKs System/Library/Frameworks directory and then just import <#Module#> the same way as you import system Frameworks.
For an OS X Playground: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks
And for iOS: /Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.0.sdk/System/Library/Frameworks/
But before you do that... go file a radar as #Rick says in his answer.
I've written a tutorial covering import of custom frameworks in Beta 6
In short, if you've got your playground and framework either in the same project or workspace, and your framework is built for 64 bits (even for iOS), they play very well together.
source: http://qiita.com/ryokosuge/items/2551cd4faa9dca324342
If there is anyone who still had difficulty understanding how to go about it after reading the accepted answer, this may help.
It's not in English though so I will explain here:
Using Carthage:
1: Setup your Cartfile in the root of the project, add your libraries and run:
carthage update --platform iOS --use-submodules
2: Make sure your project is saved as a Workspace. From the Xcode menu bar: File > Save As Workspace. Shutdown Xcode and reopen from the new .xcworkspace file.
3: Add your library's .xcodeproj file in your workspace: File > Add files to "your_workspace_name". Then find it in: ${SRCROOT}/Carthage/Checkouts/your_library_name/your_library_name.xcodeproj
4: Find the Embedded Binaries and Linked Frameworks and Libraries section in your project's general settings. Add your library's .framework file to both.
5: Clean(⇧+⌘+K) and build(⌘+B).
6: Import your library in the playground.
I solved it by copying the built frameworks to the Built Products Directory, where Playgrounds in the workspace also searches for frameworks. Note: you also need to run lipo and strip away unused architectures from the FAT binaries.
The steps needed (in a nutshell):
Create an aggregate target
Add a script phase to copy the frameworks
from Carthage/Build/iOS/ to BUILT_PRODUCTS_DIR (I use a swift script
in the example below, but you can use also use bash, ruby python
etc.)
Run aggregate target Now you can import the framework(s) in a
Playground
Additionally you can leverage the aggregate target in your app:
Embed frameworks from BUILT_PRODUCTS_DIR, not Carthage/Build/iOS/
Add aggregate target to app scheme to re-establish frameworks after a clean.
Here is an example project setup as above: https://github.com/richardnees/CarthagePlaygrounds
Like using Objective-C classes in Swift code, you can import 3rd-party library/classes into your project and Xcode will ask you if you wanna create a <#YourProjectName>-Bridging-Header header file. Choose yes and import all header files you need in that file. After that, in your playground, you can simply do import <#YourProjectName>-Bridging-Header after import UIKit and you will be able to use the classes you imported to your project.
Update: this is actually incorrect. Please see my comment below. Sorry for any confusion...

Resources