Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Invalid Swift Parseable Output (Malformed JSON)

I'm getting 4 errors for malformed JSON and one command compileSwift failed with a nonzero exit code error.

I have no clue how to debug this since it doesn't list what file this is occurring in.

screenshot of errors

I have tried deleting the workspace and pods directory and doing a new pod install && pod update.

I have tried deleting the derived data.

Neither have worked.

like image 328
mocode10 Avatar asked Feb 26 '19 18:02

mocode10


4 Answers

I was getting the same error and after reading above comments i went through my code and saw this "return 93à" So after removing this "à" it's working fine now.

like image 84
Toqir Ahmad Avatar answered Nov 19 '22 22:11

Toqir Ahmad


So here is my story on the exact same issue but a totally different cause and resolution.

TL;DR - Decode that problematic array as a string and read it, that is your real issue, not the one with JSON.

And here is my full story ...

First things first ... I got to this error by moving my app files to a framework project and changing their target.

I tried all you guys suggested, but with no luck, it just took me some time to find out how to report those files recursively. If anyone wants to check their encoding in the whole project, here is how to do that:

find . -type f -name "*.swift" -exec file {} +

All my files reported either ASCII and UTF-8, I even removed all Unicode characters to make them all ASCII, and it still didn't help.

Anyway, totally desperate, I decided on the last attempt ... trying to decode whatever was in that undecodable sequence of bytes.

I opened up my browser console, and did this:

String.fromCharCode(...[123, 10, 32, 32, 34, <the rest of the error array from XCode>])

And it saved my day, giving away the actual problem information.

What I got was the actual error message that for some reason (still unknown to me) the compiler wasn't able to process.

Here is a short extract from which you can also see why all people see the same sequence at the beginning:

{
  "kind": "finished",
  "name": "compile",
  "pid": 27181,
  "output": ...
... /RecognizedSymbolBlock.swift:5:15: error: use of undeclared type 'CGRect'\n    let rect: CGRect;\n ... 
... /RecognizedTextBlock.swift:3:7: error: type 'RecognizedTextBlock' does not conform to protocol 'Decodable'\nclass RecognizedTextBlock ...

So it turned out my problem was not having CoreGraphics framework included in the target, as well as not having it added with import CoreGraphics in the file itself.

Strangely enough, when I looked at that file in XCode (which I didn't do before as it was all just move of a code that worked before), I suddenly got all these errors displayed clearly.

My last weird finding was after I asked myself ... "Why the hell did it work in the original target without the import CoreGraphics?"

It turned out that having this in the bridging header file automatically brought it linked frameworks with it as if they were imported in all my files (it is one of the linked frameworks I use which is using UIKit):

#import <TesseractOCR/TesseractOCR.h>

But it can be anything, really, like:

#import <UIKit/UIKit.h>

The point is if you are using the bridging header file, it may easily hide the fact that you are not being forced to write consistent code with all necessary imports.

Anyway, my primary goal is to let everyone know that their original problem is most probably something totally different and the original issue is actually encoded in that error byte array everyone is getting. Even if you face encoding problems, this byte array may tell you what is wrong with your code.

Happy fixing!

like image 38
martinh_kentico Avatar answered Nov 19 '22 21:11

martinh_kentico


In the navigator pane, the reports tab (last one) is your go-to for these situations. You can see detailed logs of build actions and can track down from there.

like image 2
t0rst Avatar answered Nov 19 '22 21:11

t0rst


I had the same errors from the question. I dragged a file from project_1 to project_2 and all of a sudden all those errors appeared inside project_2. The odd thing was the new file that I dragged in had nothing to do with the errors because they appeared in completely different files that I had previously dragged in from project_1. Those files worked fine for months and the errors didn’t appear until after I dragged in the new file.

I closed Xcode, opened it back up, and the beachball of death started spinning. Xcode was basically frozen, I had to wait about 45 minutes for it to unfreeze.

I added screenshots of the errors and steps of what I had to do to resolve this issue.

1- These were the original errors, just like the ones from the op's question. I had 151 errors:

enter image description here

2- After the beachball stopped spinning I started to look through all of my files. I ran into a file that was somehow corrupted and its normal code was somehow replaced with the odd code inside the middle/right pane below.

Copying and Pasting the corrupted code from the middle/right pane didn't work at all which was odd but I did a global search (cmmnd+4) for "bookmark" and 6 more files appeared that also contained the corrupted code (shown inside the left navigation pane).

"bookmark" is the first word on line 1 inside the file in the middle/right pane, that's why I choose it:

enter image description here

3- I c+p the 6 files from the original project back into this project (the corrupted one) and the errors went from 151 to 10 new errors. All 10 errors were Invalid UTF-8 found in source file:

enter image description here

4- I looked inside all 6 files and inside the commented out code at the very top of the file there was a strange symbol in place of the copyright symbol on line 6:

enter image description here

5- I deleted the strange symbol and everything worked again.

I don't know how those files were corrupted. I think it has something to do with dragging them in from one project to another. Probably was just a random bug. To be safe I deleted the 6 files then added them back in one by one by actually creating a fresh file inside project_2’s Xcode. Then I c+p the code from same file from project_1 back to project_2. No more dragging Swift files from project to project for me.

Very strange.

like image 2
Lance Samaria Avatar answered Nov 19 '22 21:11

Lance Samaria