system.FormatException: 'Input string was not in a correct format.' in IAppversion class. Conversion from NSObject to string

huangapple go评论87阅读模式
英文:

system.FormatException: 'Input string was not in a correct format.' in IAppversion class. Conversion from NSObject to string

问题

public class Version_iOS: IAppVersion
    {
        public string GetVersion()
        {
            return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
        }
        public int GetBuild()
        {
            return int.Parse(NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString());
        }
    }
英文:

I am trying to get the build version of my iOS project using IAppVersion class. But, I am getting Exception as system.formatException while deploying to the iOS simulator through remote login to the macbook. The error is "system.FormatException: 'Input string was not in a correct format.'" Actually its my first time to deploy iOS project into simulator through remote login. I tried so many methods to resolve the issue. But, none of them are working. Please let me know is there any method to convert NSObject to string and then to Int.
Thank you.

 public class Version_iOS: IAppVersion
    {
        public string GetVersion()
        {
            return NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleShortVersionString").ToString();
        }
        public int GetBuild()
        {
            return int.Parse(NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion").ToString());

            /*NSObject k = NSBundle.MainBundle.ObjectForInfoDictionary("CFBundleVersion");
            string z = k.Description;
            return int.TryParse(z);*/
        }
    }

I tried code which is commented also. Please refer the image
[1]: https://i.stack.imgur.com/FjJwl.png

答案1

得分: 1

根据CFBundleVersion文档"该键是一个由一个到三个以句点分隔的整数组成的可机器读取的字符串,例如10.14.1。" 这意味着它很可能无法解析为整数。所以,您可以尝试将其解析为Version而不是整数:

public Version GetBuild()
{
    return Version.Parse(NSBundle.MainBundle.ObjectForInfoDictionary(
        "CFBundleVersion").ToString());
}

然而,听起来它可能会返回一个单个数字,这对于Version类来说是无效的,所以最安全的选择是将其保留为字符串:

public string GetBuild()
{
    return NSBundle.MainBundle.ObjectForInfoDictionary(
        "CFBundleVersion").ToString();
}

最后一点想法:如果您真的想返回一个整数(版本中的第一个数字),您可以像这样做:

public int GetBuild()
{
    return int.Parse(NSBundle.MainBundle.ObjectForInfoDictionary(
        "CFBundleVersion").ToString()
        .Split('.')[0]);
}
英文:

According to the CFBundleVesion documentation, "This key is a machine-readable string composed of one to three period-separated integers, such as 10.14.1."

This means that it most likely won't be able to be parsed to an int. So instead of trying to parse it to an int, you could try parsing it to a Version:

public Version GetBuild()
{
    return Version.Parse(NSBundle.MainBundle.ObjectForInfoDictionary(
        "CFBundleVersion").ToString());
}

However, it sounds like it could return a single digit, which isn't valid for the Version class, so the safest bet is to just leave it as a string:

public string GetBuild()
{
    return NSBundle.MainBundle.ObjectForInfoDictionary(
        "CFBundleVersion").ToString();
}

One final thought: if you really want to return an int (the first number in the version), you could do something like:

public int GetBuild()
{
    return int.Parse(NSBundle.MainBundle.ObjectForInfoDictionary(
        "CFBundleVersion").ToString()
        .Split('.')[0]);
}

huangapple
  • 本文由 发表于 2023年3月9日 18:27:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定