Home > OS >  Different output in Unity editor and Xcode
Different output in Unity editor and Xcode

Time:11-22

When running this code in the editor I have no problem with this piece of code but when generate executable in Xcode I get the following error:

FormatException: Input string was not in a correct format.

I am getting a string input that looks like this:

LOCAL MOON STATE * MOON PHASE= 81.6 (waning crescent)

And extract the "81.6" and convert it to a float.

I have isolated the problem and tried to solve it but not succeeded and I do not really understand what the problem is as it does work in the Unity editor (IOS).

Here is the code I use, including the Debug.Log's:

public static Sprite GetMoonPhaseSprite(string _incoming)
{
    Sprite _outgoingSprite = null;
    List<Sprite> _sprite_List = new();

    Debug.Log("\n\n\nSTART - - GetMoonPhaseSprite _incoming: "   _incoming   "\n\n\n"); // LOCAL MOON STATE * MOON PHASE= 81.6 (waning crescent)

    // waxing crescent
    pos = _incoming.IndexOf("=")   2;
    int _pos2 = _incoming.IndexOf("(") - 1;
    Debug.Log("\n\n\n_incoming: >"   _incoming.Substring(pos, _pos2 - pos)   "<\n\n\n");

    string _myInput = _incoming.Substring(pos, _pos2 - pos);
    Debug.Log("\n\n\n_myInput: >"   _myInput   "<\n\n\n");


    float _moonPhase = float.Parse(_incoming.Substring(pos, _pos2 - pos));
    Debug.Log("\n\n\n_moonPhase: >"   _moonPhase   "<\n\n\n");

Here is the output from Xcode ending with the error:

START - - GetMoonPhaseSprite _incoming: LOCAL MOON STATE * MOON PHASE= 81.6 (waning crescent)


Utility:GetMoonPhaseSprite(String)
WeatherManager:Load_BtnNr2()
<DoTheYrWeatherSearch>d__125:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,     Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object,  Task&)
System.Threading.Tasks.Task:FinishContinuations()
System.Threading.Tasks.Task`1:TrySetResult(TResult)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
<GetYrWeatherFcast>d__32:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,     Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
UnityEngine.WorkRequest:Invoke()
UnityEngine.UnitySynchronizationContext:Exec()




GetMoonPhaseSprite #1


Utility:GetMoonPhaseSprite(String)
WeatherManager:Load_BtnNr2()
<DoTheYrWeatherSearch>d__125:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,     Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object,     Task&)
System.Threading.Tasks.Task:FinishContinuations()
System.Threading.Tasks.Task`1:TrySetResult(TResult)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
<GetYrWeatherFcast>d__32:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,     Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
UnityEngine.WorkRequest:Invoke()
UnityEngine.UnitySynchronizationContext:Exec()




GetMoonPhaseSprite #2


Utility:GetMoonPhaseSprite(String)
WeatherManager:Load_BtnNr2()
<DoTheYrWeatherSearch>d__125:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,  Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object,  Task&)
System.Threading.Tasks.Task:FinishContinuations()
System.Threading.Tasks.Task`1:TrySetResult(TResult)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
<GetYrWeatherFcast>d__32:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,    Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
UnityEngine.WorkRequest:Invoke()
UnityEngine.UnitySynchronizationContext:Exec()




_incoming: >81.6<


Utility:GetMoonPhaseSprite(String)
WeatherManager:Load_BtnNr2()
<DoTheYrWeatherSearch>d__125:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,    Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object,  Task&)
System.Threading.Tasks.Task:FinishContinuations()
System.Threading.Tasks.Task`1:TrySetResult(TResult)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
<GetYrWeatherFcast>d__32:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,     Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
UnityEngine.WorkRequest:Invoke()
UnityEngine.UnitySynchronizationContext:Exec()




_myInput: >81.6<


Utility:GetMoonPhaseSprite(String)
WeatherManager:Load_BtnNr2()
<DoTheYrWeatherSearch>d__125:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,  Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
System.Threading.Tasks.AwaitTaskContinuation:RunCallback(ContextCallback, Object,  Task&)
System.Threading.Tasks.Task:FinishContinuations()
System.Threading.Tasks.Task`1:TrySetResult(TResult)
System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1:SetResult(TResult)
<GetYrWeatherFcast>d__32:MoveNext()
System.Threading.ExecutionContext:RunInternal(ExecutionContext, ContextCallback,  Object, Boolean)
System.Runtime.CompilerServices.MoveNextRunner:Run()
UnityEngine.WorkRequest:Invoke()
UnityEngine.UnitySynchronizationContext:Exec()

FormatException: Input string was not in a correct format.

CodePudding user response:

There are two possibilities.

  1. The string contains any invisible characters, use _myInput == "81.6" to make sure the string has exactly these 4 characters.

  2. In some cultures, a comma or space is used as a decimal seperator, use float.Parse(_myInput, CultureInfo.InvariantCulture) to force a dot seperator.

  • Related