UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString count]: unrecognized selector sent to instance 0x8000000000000000'

Forums > iOS

Hello all,

When opening a particular view, my app sometimes crashes, causing the following to appear in the debug area:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber count]: unrecognized selector sent to instance 0x8000000000000000'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000018048d8a8 __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x000000018008409c objc_exception_throw + 56
    2   CoreFoundation                      0x00000001804a26f8 +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   CoreFoundation                      0x00000001804919f8 ___forwarding___ + 1280
    4   CoreFoundation                      0x0000000180493d1c _CF_forwarding_prep_0 + 92
    5   libswiftCore.dylib                  0x0000000192704298 $sSD8_VariantV8setValue_6forKeyyq_n_xtF + 96
    6   libswiftCore.dylib                  0x00000001926c4c9c $sSDyq_Sgxcis + 296
    7   CoreTransferable                    0x00000001bfa0cdd0 $s16CoreTransferable17__TupleDescriptorPAAE16tupleDescriptionyAA02__c4TypeF0VyxGAA02__cG0VFZ + 248
    8   CoreTransferable                    0x00000001bfa0d13c $s16CoreTransferable27TupleTransferRepresentationV011_decomposeddE0_6inputsAA01_dE7OutputsVAA01_dE5ValueVyACyxq_GG_AA01_dE6InputsVtFZ + 224
    9   CoreTransferable                    0x00000001bfa1822c $sSo14NSItemProviderC16CoreTransferableE08registerD0yyxyYbXAYaAC0D0RzlFTY0_ + 200
    10  SwiftUI                             0x00000001c4805a8d objectdestroyTm + 3189
    11  SwiftUI                             0x00000001c4806ccd objectdestroy.23Tm + 1665
    12  SwiftUI                             0x00000001c4804689 OUTLINED_FUNCTION_39 + 3937
    13  SwiftUI                             0x00000001c4e6366d OUTLINED_FUNCTION_16 + 2217
    14  SwiftUI                             0x00000001c4e63965 OUTLINED_FUNCTION_16 + 2977
    15  SwiftUI                             0x00000001c4660705 OUTLINED_FUNCTION_9 + 131069
    16  SwiftUI                             0x00000001c4ec8a61 objectdestroy.29Tm + 2265
    17  libswift_Concurrency.dylib          0x00000001e2c399c5 _ZL23completeTaskWithClosurePN5swift12AsyncContextEPNS_10SwiftErrorE + 1
)
libc++abi: terminating due to uncaught exception of type NSException

The view that is causing the crash when opened:

import SwiftUI
import Charts

struct Graph: View {
    @Binding var coordinates: [CGPoint]
    let physicalQuantity: (x: String, y: String)
    @Binding var showCoords: Bool
    @Binding var isSelected: Bool
    @Binding var selectedCoordinateIndex: Int

    init(coordinates: Binding<[CGPoint]>, physicalQuantity: (x: String, y: String), showCoords: Binding<Bool> = Binding.constant(false), isSelected: Binding<Bool> = Binding.constant(false), selectedCoordinateIndex: Binding<Int> = Binding.constant(0)) {
        self._coordinates = coordinates
        self.physicalQuantity = physicalQuantity
        self._showCoords = showCoords
        self._isSelected = isSelected
        self._selectedCoordinateIndex = selectedCoordinateIndex
    }

    @State private var graphToShare = Image(systemName: "photo")

    @MainActor func renderGraphImg() {
        let renderer = ImageRenderer(content: self.chart.frame(width: 300))

        if let uiImage = renderer.uiImage {
            graphToShare = Image(uiImage: uiImage)
        }
    }

    var body: some View {
        VStack(alignment: .leading) {
            ShareLink("Share graph", item: graphToShare, preview: SharePreview("Graph", image: graphToShare))
                .onAppear {
                    renderGraphImg()
                }

            chart
        }
    }

    var chart: some View {
        Chart(coordinates, id: \.x) { coordinate in
            LineMark(
                x: .value(physicalQuantity.x, Double(coordinate.x)),
                y: .value(physicalQuantity.y, Double(coordinate.y))
            )
            .interpolationMethod(.catmullRom)
            .foregroundStyle(.red)

            .symbol {
                if showCoords {
                    Circle()
                        .frame(width: 6, height: 6)
                        .foregroundStyle(selectedCoordinateEquals(coordinate) && isSelected ? .blue : .red)
                }
            }
        }
        .chartXAxisLabel(physicalQuantity.x)
        .chartYAxisLabel(physicalQuantity.y)
        .chartYAxis {
            AxisMarks(position: .leading)
        }
        .frame(height: UIDevice.current.userInterfaceIdiom == .phone ? 200 : 400)
    }

    func selectedCoordinateEquals(_ other: CGPoint) -> Bool {
        if selectedCoordinateIndex < coordinates.count {
            if coordinates[selectedCoordinateIndex].equalTo(other) {
                return true
            }
        }
        return false
    }
}

The weird thing is that it doesn't always crash when I open that view. I think it might have something to do with CoreData, because I use it with coordinates that are saved in there. Though the view I fetch the data in seems to work fine.

Does anyone have a clue on what causes this error? Thank you in advance!

3      

Note: After trying a few things, I just found out that removing the renderGraphImg() function stops the app from crashing. Still I'd really want to know what exactly caused the trouble, because removing the renderGraphImg() function means removing an important functionality of my app.

3      

Found out that making the graphToShare variable optional and setting it's start value to nil (and unwrapping it in the view's body) solved the problem. But I don't know why this solution works...

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.