I'm sure the solution must be quite simple, but I can't figure out how to extract the value from a SmartStore Aggregate function query using mobile sdk 7.2 in Swift.

Here's my query:

        let sql = "select sum({inventory:qtyOnHand}) from {inventory} where {inventory:location} = '1234'"
        if let querySepc = QuerySpec.buildSmartQuerySpec(smartSql: sql , pageSize: 1){
            let store = SmartStore.shared(withName: SmartStore.defaultStoreName)!

            let qryResult: [Any] = try store.query(using: querySepc, startingFromPageIndex: 0)
            print("qryResult = \(qryResult)") 
            //prints qryResult = [<__NSArrayM 0x600003d88f60>(432)]
            print(type(of: qryResult))
            //prints Array<Any>            

            let record = qryResult[0]

            print("record = \(record)")
            //prints record = (432)
            print("type of record = \(type(of:record))")
            //prints type of record = __NSArrayM

I've tried casting qryResult[0] to [String:Any], [Int:Any] any of these variations just sets the the record constant to nil

I tried accessing the value from the record constant via subscript and that failed with: "Value of type 'Any' has no subscripts"

  • Is this question is even related to Salesforce?
    – SatyaV
    Commented Aug 28, 2019 at 16:43
  • @ArnoldJr., it is related to the Salesforce Mobile SDK
    – aspjim
    Commented Aug 29, 2019 at 15:38

1 Answer 1


When you use SmartSQL, you get back an array of arrays. So if you do:

select a,b,c from x

You should get back something like:

[[a1,b1,c1], [a2,b2,c3], ...]

Therefore if you do:

select sum(a) from x

You should get back something like:


Example in Swift

let query = QuerySpec.buildSmartQuerySpec(smartSql: "SELECT count(*) FROM {\(SOUP_NAME)}", pageSize: 1)!
let result = try self.store.query(using:query, startingFromPageIndex:0);
let count = (result[0] as! [Int])[0];

For an example, check out https://github.com/forcedotcom/SalesforceMobileSDK-iOS/blob/master/libs/SmartStore/SmartStoreTests/SFSmartSqlTests.m#L220

For a Swift example, check out https://github.com/wmathurin/GlobalStoreTester/blob/master/common/ViewController70.swift#L97

  • Hi Wolfgang thank you for the input! I must be accessing the data incorrectly? it seems like I am only getting back an array of Any [Any] instead of an array of arrays [[Any]]. When I print out the result of the query “select count({contacts:Id}) from {contacts}”, this is what I get in the output window of Xcode 10.3: [<__NSArrayM 0x60000334c930>( 1 ) ] If I assign subscript 0 of the query result to a variable I am then left with an Any with no subscripts per the compiler. If I try and cast the Any to an Int it is nil.
    – aspjim
    Commented Aug 30, 2019 at 17:43
  • I added a swift example in my answer. Hope that helps. Commented Sep 4, 2019 at 22:40
  • Thanks Wolfgang, that worked. Very much appreciate the assistance!
    – aspjim
    Commented Sep 5, 2019 at 18:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .