How to Cast an _Nsmallocblock_ to Its Underlying Type in Swift 3

Can I specify a objectId when creating new PFObjects?

No you can not. Parse sets the objectId on the server during the save operation.

The reason your operation is failing is because Parse is looking for an object on the server with the id that you are specifying and is then trying to update that object but it cannot find the object.

Trigger UIAlertAction on UIAlertController programmatically?

Here's roughly what I did:

  1. Created a mocked version of my class that would present the alert controller, and in my unit tests, used this mock.

  2. Overrode the following method that I'd created in the non-mocked version:

    func alertActionWithTitle(title: String?, style: UIAlertActionStyle, handler: Handler) -> UIAlertAction
  3. In the overridden implementation, stored all the details about the actions in some properties (Handler is just a typealias'd () -> (UIAlertAction))

    var didCreateAlert = false
    var createdTitles: [String?] = []
    var createdStyles: [UIAlertActionStyle?] = []
    var createdHandlers: [Handler?] = []
    var createdActions: [UIAlertAction?] = []
  4. Then, when running my tests, to traverse the path through the alerts, I implemented a callHandlerAtIndex method to iterate through my handlers and execute the right one.

This means that my tests look something like this:

feedback.start()
feedback.callHandlerAtIndex(1) // First alert, second action
feedback.callHandlerAtIndex(2) // Second alert, third action
XCTAssertTrue(mockMailer.didCallMail)

Could not cast value of type 'Swift.Array Any ' to 'Swift.Dictionary Swift.String, Any '

When you call myGoalieInforamtionCell.fillTableView you are passing [myShotArray] - those square brackets mean that you have put myShotArray inside another array, so what you are actually passing to fillTableView is [[[String:Any]]] - an array of array of dictionary.

You can fix your immediate problem by simply removing those brackets;

myGoalieInforamtionCell.fillTableView(with: myShotArray)

However, you have way too many Any's there. You should take advantage of Swift's strong typing, which will avoid this sort of error.

I would suggest that you use a Struct rather than a dictionary for your data and then you can type things correctly. Something like:

enum Period {
case first
case second
case third
case fourth
}

struct ShotInfo {
let shotNumber: Int
let location: String // Not sure what this type should be
let timeOfShot: Date
let period: Period
let result: Bool
}

var myShotArray = [ShotInfo]()

let shot = ShotInfo(shotNumber: myShotsOnNet, location: shot, timeOfShot: Date(), period: .first, result: true}

myShotArray.append(shot)

myGoalieInforamtionCell.fillTableView(with: myShotArray)

func fillTableView(with array: [ShotInfo]) {
myShotArray = array
tableView.reloadData()

print("myShotArray \(myShotArray)")
}

If you have this and you mistakenly said fillTableView(with: [myShotArray]) Xcode will tell you straight away that you have a mismatch between the argument type and the expected type which is much better than discovering your error at run time when your program crashes.

In swift, can a function be a type?

Every function has a specific function type, made up of the parameter types and the return type of the function.

typealias FooType = (Int, String)->String

func foo(i: Int, s: String)->String {
return s + "_\(i)"
}

func bar(foo0: FooType)->String {
return foo0(100, "alpha")
}

print(bar(foo)) // alpha_100

let f:FooType = { i, s in
return s + "_\(i)"
}

print(f(200, "beta")) // beta_200

an appendix, especially for Brandon :-)

let farr:[()->Int] = [{return 1}, {return 2}, {return 3}]
for f in farr {
print(f())
/*
1
2
3
*/
}

appendix 2, for Brandon

func f1()->Int {
return 1
}
func f2()->Int {
return 2
}

let farr2:[()->Int] = [f1, f2]
for f in farr2 {
print(f())
/*
1
2
*/
}

app 3 :-)

let farr2 = [f1, f2]
for f in farr2 {
print(f())
/*
1
2
*/
}

from apple docs

Every function in Swift has a type, consisting of the function’s
parameter types and return type. You can use this type like any other
type in Swift, which makes it easy to pass functions as parameters to
other functions, and to return functions from functions. Functions can
also be written within other functions to encapsulate useful
functionality within a nested function scope.



Related Topics



Leave a reply



Submit