Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSJSONSerialization generating NSCFString* and NSTaggedPointerString*

Executing NSJSONSerialization on the following json sometimes gives me NSCFString* and sometimes NSTaggedPointerString* on string values. Does anyone know why this is the case and what NSJSONSerialization uses to determine which type it returns?

jsonData = [NSJSONSerialization JSONObjectWithData:data
                                                   options:kNilOptions
                                                     error:&parseError];

    {
      "UserPermissionsService": {
        "ServiceHeader": {}, 
        "UserApplicationPermissions": {
          "ApplicationPermissions": {
            "ApplicationID": "TEST", 
            "Permission": [
              {
                "Locations": [
                  "00000"
                ], 
                "PermissionID": "LOGIN"
              }, 
              {
                "Locations": [
                  "00000"
                ], 
                "PermissionID": "SALES_REPORT_VIEW"
              }
            ]
          }
        }
      }
    }

"LOGIN" comes back as a NSTaggedPointerString*. "SALES_REPORT_VIEW" comes back is a NSCFString*. This is having an impact downstream where I'm using and casting the values.

UPDATE

Here's what I learned...

"NSTaggedPointerString results when the whole value can be kept in the pointer itself without allocating any data."

There's a detailed explanation here...

https://www.mikeash.com/pyblog/friday-qa-2015-07-31-tagged-pointer-strings.html

Since NSTaggedPointerString is a subclass of NSString showing quotes / not showing quotes should never been an issue for me as the data is used.

Thanks for everyone that commented. I'm comfortable I understand what NSJSONSerialization is doing.

like image 680
Justin Domnitz Avatar asked Oct 13 '15 14:10

Justin Domnitz


1 Answers

Much of Foundation is implemented as class clusters. TL;DR you interact with it as an NSString but foundation will change the backing implementation to optimize for certain performance or space characteristics based on the actual contents.

If you are curious one of the Foundation team dumped a list of the class clusters as of iOS 11 here

like image 174
tapi Avatar answered Oct 20 '22 00:10

tapi