Skip to content

Commit

Permalink
Mo/swift example (#10)
Browse files Browse the repository at this point in the history
* initial setup of project

* testing bridge headeers

* swift trial

* adding swift project with objective c headers

* swift

* update readme

* added tableview to the swift project

* documentation and Readme update

* Update README.md
  • Loading branch information
firetix authored Sep 12, 2019
1 parent c164750 commit 704964f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,64 @@ If you are trying to use the Objective-C Pixlee API with a Swift project please
#import <pixlee_sdk/PXLAlbum.h>
```

1. These files are now accessible across all your Swift code and can be use the same way as before.

### Swift example

To load an album from a sku number you can run the following Swift code, please check the swift_example project directory:
```
let album: PXLAlbum = PXLAlbum(skuIdentifier: PXLSkuAlbumIdentifier)
let filterOptions:PXLAlbumFilterOptions = PXLAlbumFilterOptions()
album.filterOptions = filterOptions
// Create and set sort options on the album.
let sortOptions = PXLAlbumSortOptions()
sortOptions.sortType = PXLAlbumSortType.random
sortOptions.ascending = true
album.sortOptions = sortOptions
album.perPage = 1
album.loadNextPageOfPhotos(fromSku: { photos, error in
if let error = error {
print("\(error)")
}
print(type(of: photos))
if photos?.count != nil {
// var indexPaths: [AnyHashable] = []
// var firstIndex: Int? = nil
if let arr = photos as? Array<PXLPhoto> {
for p in arr{
print(p.cdnLargeUrl)
}
print(arr)
}
}
})
```

#### Swift type casting

Unfortunately the type casting is not fully working when using Objective-C libraries. You will have to cast the return object from the PIxlee API manually like so ``` let arr = photos as? Array<PXLPhoto>```

Check the snipet of code for a full version:

```
album.loadNextPageOfPhotos(fromSku: { photos, error in
if let error = error {
print("\(error)")
}
print(type(of: photos)) # Optional<Array<Any>>
if let arr = photos as? Array<PXLPhoto> {
}
})
```


### Important
If you are using xcode 10, the new build system doesn't work with the example project. A temporary workaround seems to be switching to the legacy build system by going to (in Xcode) File -> Workspace Settings -> Build System -> Legacy Build System. But compiling with the CLI still doesnt work.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

let PXLAlbumIdentifier = "4515393"

let PXLSkuAlbumIdentifier = "300152"


let album: PXLAlbum = PXLAlbum(skuIdentifier: PXLSkuAlbumIdentifier)
let filterOptions:PXLAlbumFilterOptions = PXLAlbumFilterOptions()
var dict = [AnyHashable : Any](minimumCapacity: 10)

album.filterOptions = filterOptions

// Create and set sort options on the album.
Expand All @@ -35,8 +37,7 @@ class FirstViewController: UIViewController {
}
print(type(of: photos))
if photos?.count != nil {
var indexPaths: [AnyHashable] = []
var firstIndex: Int? = nil

if let arr = photos as? Array<PXLPhoto> {
for p in arr{
print(p.cdnLargeUrl)
Expand Down
15 changes: 2 additions & 13 deletions pixlee_sdk/PXLAlbum.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
@param completionBlock A block called after the photos have been loaded or an error has occurred.
`photos` will contain the photos loaded in this network call.
@return The `NSURLSessionDataTask` used to load the data from the server.
@return The `NSURLSessionDataTask` used to load the data from the server. With two parameters NSarray<PXLPhoto> and NSerror, if using swift cast it to the correct type Check Readme.
*/
- (NSURLSessionDataTask *)loadNextPageOfPhotos:(void (^)(NSArray *photos, NSError *error))completionBlock;

Expand All @@ -114,7 +114,7 @@
@param completionBlock A block called after the photos have been loaded or an error has occurred.
`photos` will contain the photos loaded in this network call.
@return The `NSURLSessionDataTask` used to load the data from the server.
@return The `NSURLSessionDataTask` used to load the data from the server. With two parameters NSarray<PXLPhoto> and NSerror, if using swift cast it to the correct type Check Readme.
*/
- (NSURLSessionDataTask *)loadNextPageOfPhotosFromSku:(void (^)(NSArray *photos, NSError *error))completionBlock;

Expand Down Expand Up @@ -155,17 +155,6 @@ Call this whenever a user clicks 'Load More' button on the widget



///---------------------
/// @name Analytics Event
///---------------------

/**
Call this whenever a user clicks 'Load More' button on the widget
@return The `NSURLSessionDataTask` the server will retun 'OK' if accepted.
*/
- (NSURLSessionDataTask *)triggerEventLoadMoreClicked:(void (^)(NSError *error))completionBlock;


@end

0 comments on commit 704964f

Please sign in to comment.