Parse & Xcode 6.4 problems unwrapping optionals with Swift 1.2 - swift

I'm having trouble unwrapping an optional after upgrading to xcode 6.4. Here's the error I'm getting when running this line of code:
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
I know that with the new update for xcode you have to be more careful unwrapping optionals, but I'm not sure what changes I need to make to make swift cooperate. I've tried many different ways that were suggested on S/O but haven't gotten one to work yet. Here's my full code:
Thanks!!
import UIKit
import Parse
class TableViewController: UITableViewController {
var usernames = [""]
var userids = [""]
var isFollowing = ["":false]
override func viewDidLoad() {
super.viewDidLoad()
var query = PFUser.query()
query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let users = objects {
self.usernames.removeAll(keepCapacity: true)
self.userids.removeAll(keepCapacity: true)
self.isFollowing.removeAll(keepCapacity: true)
for object in users {
if let user = object as? PFUser {
if user.objectId! != PFUser.currentUser()?.objectId {
self.usernames.append(user.username!)
self.userids.append(user.objectId!)
var query = PFQuery(className: "followers")
***query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)***
query.whereKey("following", equalTo: user.objectId!)
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
if objects.count > 0 {
self.isFollowing[user.objectId!] = true
} else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.usernames.count {
self.tableView.reloadData()
}
})
}
}
}
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = usernames[indexPath.row]
let followedObjectId = userids[indexPath.row]
if isFollowing[followedObjectId] == true {
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
let followedObjectId = userids[indexPath.row]
if isFollowing[followedObjectId] == false {
isFollowing[followedObjectId] = true
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
var following = PFObject(className: "followers")
following["following"] = userids[indexPath.row]
following["follower"] = PFUser.currentUser()?.objectId
following.saveInBackground()
} else {
isFollowing[followedObjectId] = false
cell.accessoryType = UITableViewCellAccessoryType.None
var query = PFQuery(className: "followers")
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
query.whereKey("following", equalTo: userids[indexPath.row])
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
for object in objects {
object.deleteInBackground()
}
}
})
}
}
}

You can make use of Optional Binding to access the logged in user to make sure that it is not nil before using it as a query parameter.
So you should change the line:
query.whereKey("follower", equalTo: PFUser.currentUser()!.objectId!)
With:
if let userObj = PFUser.currentUser()?.objectId {
query.whereKey("follower", equalTo: userObj)
}
Now this will solve your bug and prevent your app from crashing, but maybe you should restructure your app in a way to confirm that the user has logged in no matter what when reaching this UITableViewController.
In any case you should still use Optional Binding to prevent a potential crash.

Related

Query within query not appending with Swift/Parse

I hope someone can help because this is probably a simple problem, but my Parse query within a query prints the object I am looking for, but cannot append it to the array I need it in to retrieve it in a UITableView.
The error is "fatal error: index out of range" when I dequeue the results of the query in a cell.
Here's the code:
import UIKit
import Parse
import Bolts
class MessagesTableVC: UITableViewController {
var usernames = [String]()
var sentDate = [NSDate]()
var details = [String]()
var userImage = [PFFile]()
#IBAction func backToProfile(sender: AnyObject) {
self.performSegueWithIdentifier("messagesToProfile", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
let messagesQuery = PFQuery(className: "Messages")
messagesQuery.whereKey("recipientId", equalTo: PFUser.currentUser()!.objectId!)
messagesQuery.includeKey("senderId")
messagesQuery.orderByDescending("createdAt")
messagesQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error != nil {
print(error)
}
if error == nil {
if let objects = objects {
self.usernames.removeAll(keepCapacity: true)
self.sentDate.removeAll(keepCapacity: true)
self.details.removeAll(keepCapacity: true)
self.userImage.removeAll(keepCapacity: true)
for object in objects {
self.sentDate.append(object.createdAt! as NSDate)
if (object["item"] != nil) {
self.details.append(object["item"] as! String)
} else {
self.details.append(object["request"] as! String)
}
let senderObject = (object["senderId"] as! PFUser)
let senderId = (senderObject.objectId! as String)
print(senderId)
// Query for sender info
let userQuery = PFUser.query()
userQuery?.whereKey("objectId", equalTo: senderId)
userQuery?.getFirstObjectInBackgroundWithBlock({ (object, error) in
self.usernames.append((object!["username"] as! String))
//self.userImage.append(object!["profilePicture"] as! PFFile)
})
}
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
//self.search.resignFirstResponder()
}
}
}
})
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sentDate.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as! MessageTableViewCell
//print(usernames[indexPath.row])
cell.senderUsername.text = usernames[indexPath.row]
cell.itemOrPreview.text = details[indexPath.row]
let date = sentDate[indexPath.row]
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .ShortStyle
let dateString = formatter.stringFromDate(date)
cell.sentDate.text = dateString
//userImage[indexPath.row].getDataInBackgroundWithBlock { (data, error) in
// if let downloadedItemImage = UIImage(data: data!) {
// cell.senderImage?.image = downloadedItemImage
//}
//}
return cell
}
override func viewWillAppear(animated: Bool) {
tableView.reloadData()
}
}
As I may guess, you might get inconsistent number of elements in sentDate and usernames because you append to usernames via asynchronous getFirstObjectInBackgroundWithBlock method, so, by the time you call reloadData on tableView, all user names might not be yet added to usernames. You might have less items in usernames then in sentDate by the time your tableView callback fires and in numberOfItems you return number of items in sentDate.
In order to fix that you need at first refactor your code, it has a lot of places things might go wrong. I won't give any specific advise, but, seems, you might want to wait before you get all the data before you reload your tableView.

Definition conflicts with previous value - also, when is override deleted?

So I am attempting to make a tableview out of an array of cloudkit downloaded items. However, I'm having this simple little "definition conflicts with pervious value" error. This error occurs for the cellForRowAtIndexPath function - which supposedly conflicts with the numberOfRowsInSection function. I have attempted moving/deleting brackets of the latter function and by placing a question mark/optional at the end... "-> UITableViewCell?" to no avail. Where could the error be coming from?
Also, as it stands, xcode deleted the override portion of each tableview function. Why does it sometimes stay and when is it deleted?
import UIKit
import CloudKit
class DiningTable: UITableViewController {
var categories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
func getRecords()
{
categories = []
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["Name", "Address", "Picture"]
queryOperation.qualityOfService = .UserInteractive
queryOperation.recordFetchedBlock = { (record:CKRecord) -> Void in
let categoryRecord = record
self.categories.append(categoryRecord)
}
queryOperation.queryCompletionBlock = { (cursor:CKQueryCursor?, error: NSError?) -> Void in
if (error != nil) {
print("Failed to get data from iCloud - \(error!.localizedDescription)")
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
publicDatabase.addOperation(queryOperation
}
func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
return self.categories.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! DiningTableCell
let restaurant: CKRecord = categories[indexPath.row]
cell.RestaurantName?.text = restaurant.valueForKey("Name") as? String
let img = restaurant.objectForKey("Picture") as! CKAsset
cell.RestaurantPhoto.image = UIImage(contentsOfFile: img.fileURL.path!)
return cell
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue1" {
if let destViewController = segue.destinationViewController as? RestaurantTable {
let indexPath = self.tableView.indexPathForSelectedRow!
destViewController.indexpath1 = indexPath
}
}
}
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
}
Your entire class definition is nested inside your
override func viewDidLoad() {
function. Put a closing brace in after the call to super.viewDidLoad()
Are you sure that's your code or could you have accidentally removed some lines?
Because, as it stands, your table view functions are actually embedded in your viewDidLoad function... They need to be object level functions.
Note the indentation level of your code when you have Xcode indent the code for you (Right-click on the screen and select Structure->Re-indent.
import UIKit
import CloudKit
class DiningTable: UITableViewController {
var categories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
} // <--- you are missing this close brace here.
func getRecords()
{
categories = []
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
let queryOperation = CKQueryOperation(query: query)
queryOperation.desiredKeys = ["Name", "Address", "Picture"]
queryOperation.qualityOfService = .UserInteractive
queryOperation.recordFetchedBlock = { (record:CKRecord) -> Void in
let categoryRecord = record
self.categories.append(categoryRecord)
}
queryOperation.queryCompletionBlock = { (cursor:CKQueryCursor?, error: NSError?) -> Void in
if (error != nil) {
print("Failed to get data from iCloud - \(error!.localizedDescription)")
dispatch_async(dispatch_get_main_queue(), {
self.tableView.reloadData()
})
}
}
publicDatabase.addOperation(queryOperation)
}
override func tableView(tableView: UITableView, numberOfRowsInSection: Int) -> Int {
return self.categories.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! DiningTableCell
let restaurant: CKRecord = categories[indexPath.row]
cell.RestaurantName?.text = restaurant.valueForKey("Name") as? String
let img = restaurant.objectForKey("Picture") as! CKAsset
cell.RestaurantPhoto.image = UIImage(contentsOfFile: img.fileURL.path!)
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "segue1" {
if let destViewController = segue.destinationViewController as? RestaurantTable {
let indexPath = self.tableView.indexPathForSelectedRow!
destViewController.indexpath1 = indexPath
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

CloudKit - Fetching 5 records with one field type... takes 3-4 seconds. why is it so slow?

I have a recordtype - "DiningTypes". "DiningTypes only has one field type, which is a string. I have 5 records... and it takes 3-4 second to load a table. How is that this is so slow? Do I need to begin the fetching process in a previous controller to have quicker UI response times?
import UIKit
import CloudKit
class table1: UITableViewController {
var categories: Array<CKRecord> = []
var fetchedcategories: Array<CKRecord> = []
override func viewDidLoad() {
super.viewDidLoad()
func fetchdiningtypes()
{
let container = CKContainer.defaultContainer()
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
let query = CKQuery(recordType: "DiningTypes", predicate: predicate)
publicDatabase.performQuery(query, inZoneWithID: nil) { (results, error) -> Void in
if (error != nil)
{
print("Error" + (error?.localizedDescription)!)
}
else
{
for result in results!
{
self.categories.append(result)
}
}
}
}
fetchdiningtypes()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return categories.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("dining") as! table1cell
let restaurant: CKRecord = categories[indexPath.row]
cell.Name.text = restaurant.valueForKey("Name") as? String
return cell
}
}
The default quality of service for CloudKit operations is NSQualityOfServiceUtility. If your application isn't in the foreground when the request runs you may see operations take longer than usual to run.
Try using CKQueryOperation and setting its qualityOfService to NSOperationQualityOfServiceUserInitiated.

Tutorial in retrieving, mutating and saving array from Parse.com in Swift with UITableView

import UIKit
class FeedTableViewController: UITableViewController {
var navBar:UINavigationBar=UINavigationBar()
let font = UIFont(name: "Baskerville", size: 15)
var feedData:NSMutableArray = NSMutableArray()
required init(coder aDecoder: NSCoder){
super.init(coder: aDecoder)
}
#IBAction func likeButton(sender: AnyObject) {
if var votes:Int? = quote!.objectForKey("votes") as? Int {
votes!++
}
}
#IBAction func loadData(sender: AnyObject?) {
feedData.removeAllObjects()
var findFeedData:PFQuery = PFQuery(className: "userQuotes")
findFeedData.findObjectsInBackgroundWithBlock{
(objects:[AnyObject]?, error:NSError?)->Void in
if error == nil{
if let objs = objects{
for object in objs{
let quote:PFObject = object as! PFObject
self.feedData.addObject(quote)
// let user:PFUser = (object as! NSArray).lastObject as! PFUser
}
//println(self.feedData)
let array:NSArray = self.feedData.reverseObjectEnumerator().allObjects
self.feedData = NSMutableArray(array: array)
NSOperationQueue.mainQueue().addOperationWithBlock({
self.tableView.reloadData()
})
}
}
}
}
override func viewDidAppear(animated: Bool) {
self.loadData( nil )
}
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Quotezilla"
// 3
//self.navigationItem.setRightBarButtonItem(rightSearchBarButtonItem, animated: true)
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return feedData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:QuoteTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! QuoteTableViewCell
let quote:PFObject = self.feedData.objectAtIndex(indexPath.row) as! PFObject
cell.contentTextView!.font = font
cell.timeStampLabel!.font = font
cell.publisherLabel!.font = font
cell.contentTextView.alpha = 0
cell.timeStampLabel.alpha = 0
cell.publisherLabel.alpha = 0
cell.contentTextView.text = quote.objectForKey("content") as! String
//cell.publisherLabel.text = quote.objectForKey("publisher") as? String
/* func loadLikes(){
if var votes:Int? = quote.objectForKey("votes") as? Int {
votes!++
}
}*/
var dateFormatter:NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, h:mm a"
cell.timeStampLabel.text = dateFormatter.stringFromDate(quote.createdAt!)
var votes:Int? = quote["votes"] as? Int
if votes == nil {
votes = 0
}
cell.likesLabel?.text = "\(votes!)"
var myObject = quote["publisher"] as? PFObject
myObject?.fetchIfNeeded()
if let foundUser = myObject as? PFUser{
cell.publisherLabel.text = foundUser.username
UIView.animateWithDuration(0.7, animations: {
cell.contentTextView.alpha = 1
cell.timeStampLabel.alpha = 1
cell.publisherLabel.alpha = 1
})
}
return cell
}
So what I am essentially attempting to do is create a likes or votes button. As you see in the code I have a likeButton action that is supposed to auto-increment the likes section in parse. I display the current likes that I have filled into the rows in Parse itself in the cellForRowAtIndexPath function. The problem is that I cannot call quote.objectForKey("votes"), because I initialize it later. I have been poring over this problem and cannot find a way to make the votes update in parse through the likeButton action.
You must live with life on the network. That means your table won't have certain data available when the App starts. Handle a missing object or missing key within a particular cell gracefully and just use some kind of placeholder value. When the parse callback executes, you are already correctly forcing a refresh.
OK So BIG EDIT
This class needed a lot of work. I'm not even going to spell out every change here, but it's basically a complete Parse.com tutorial at this point.
This code compiles cleanly but I can't be sure of everything in your context. In particular do you have a 'likesButton' on every table row as part of your custom table cell view? I'm assuming that.
class FeedTableViewController: UITableViewController {
var navBar = UINavigationBar()
let font = UIFont(name: "Baskerville", size: 15)
var feedData = [PFObject]()
static let cellID = "cell"
// NOTE! See how this tag is set below
#IBAction func likeButton(sender: UIButton) {
let quote = feedData[sender.tag]
if let votes = quote.objectForKey("votes") as? Int {
quote.setObject(votes + 1, forKey: "votes")
}
else {
// CHALLENGE FOR YOU: handle the case of no votes attribute
}
// UPDATE the local UI
tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: sender.tag, inSection: 0)],
withRowAnimation: .None)
// CHALLENGE FOR YOU: UPDATE Parse...start a new question if necessary
}
#IBAction func loadData(sender: AnyObject?) {
feedData.removeAll()
PFQuery(className: "userQuotes").findObjectsInBackgroundWithBlock {
[unowned self]
(objects: [AnyObject]?, error: NSError?) -> Void in
if let objs = objects {
for object in objs {
self.feedData.append(object as! PFObject)
}
self.feedData = self.feedData.reverse()
}
NSOperationQueue.mainQueue().addOperationWithBlock { self.tableView.reloadData() }
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.loadData(nil)
self.title = "Quotezilla"
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return feedData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(FeedTableViewController.cellID, forIndexPath: indexPath) as! QuoteTableViewCell
cell.likesButton!.tag = indexPath.row // See how tag works with the above
cell.contentTextView!.font = font
cell.timeStampLabel!.font = font
cell.publisherLabel!.font = font
cell.contentTextView.alpha = 0.0
cell.timeStampLabel.alpha = 0.0
cell.publisherLabel.alpha = 0.0
let q = feedData[indexPath.row]
if let content = q.objectForKey("content") as? String {
cell.contentTextView.text = content
}
else {
cell.contentTextView.text = "Content not found!"
}
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, h:mm a"
cell.timeStampLabel.text = dateFormatter.stringFromDate(q.createdAt!)
let votes = (q.objectForKey("votes") as? Int) ?? 0
cell.likesLabel?.text = "\(votes)"
let myObject = q.objectForKey("publisher") as? PFObject
myObject?.fetchInBackgroundWithBlock {
[unowned self]
(object: PFObject?, error: NSError?) in
NSOperationQueue.mainQueue().addOperationWithBlock {
if let foundUser = object as? PFUser {
cell.publisherLabel.text = foundUser.username
UIView.animateWithDuration(0.7) {
cell.contentTextView.alpha = 1.0
cell.timeStampLabel.alpha = 1.0
cell.publisherLabel.alpha = 1.0
}
}
else {
cell.publisherLabel.text = "Publisher not found!"
}
}
}
return cell
}
}

Collection View Controller crashing and not returning images

I am building a Collection View to return images from a CloudKit database. The results are returning slowly, but they are returning. However, I am getting the classic "fatal error: unexpectedly found nil while unwrapping an Optional value" error and I can't figure out why or what I have to fix to solve the error. Does anyone have any suggestions on how I can fix this problem?
My code is as follows:
class CollectionViewController: UICollectionViewController {
#IBOutlet var myCollectionView: UICollectionView!
var image = UIImage()
var matchedSelfie = [UIImage]()
let reuseIdentifier = "MyCell"
override func viewDidLoad() {
super.viewDidLoad()
// self.canDisplayBannerAds = true
println("Loading")
var countedImages = matchedSelfie
let publicDB = CKContainer.defaultContainer().publicCloudDatabase
print(publicDB)
let data = CKRecord(recordType: "theUsers")
let pred = NSPredicate(format: "TRUEPREDICATE")
let myQuery = CKQuery(recordType: "theUsers", predicate: pred)
publicDB.performQuery(myQuery, inZoneWithID: nil) {
results, error in
println("Results from publicDB Query: \(results)")
if error != nil {
println(error)
} else {
for record in results {
if var aselfie = record.objectForKey("selfie") as CKAsset! {
if var url = aselfie.fileURL {
var imageData = UIImage(contentsOfFile: url.path!)!
self.matchedSelfie.append(imageData)
}
println(self.matchedSelfie)
println("total selfies: \(self.matchedSelfie.count)")
var cellscount = self.matchedSelfie.count
}
}
}
println("Done Loading")
self.myCollectionView.reloadData()
}
}//View Did Load
func collectionView(collectionView: UICollectionView, numberOfSectionsInCollectionView: Int) -> Int {
self.myCollectionView.reloadData()
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection: Int) -> Int {
self.myCollectionView.reloadData()
println("Selfies: \(self.matchedSelfie.count)")
println(matchedSelfie.count)
myCollectionView.reloadData()
return self.matchedSelfie.count
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCell", forIndexPath: indexPath) as CollectionViewCell
println("just seeing when this prints")
cell.amatchedSelfie.image = image
return cell
}
}
Do you know which line it fails? And what is the value of image at time of assigning it to cell.amatchedSelfie.image? Probably you can check for nil before assigning to cell.amatchedSelfie.image