shiter
2015-06-29 09:39
采纳率: 96.4%
浏览 2.7k

ios swift string 类型转化为CGfloat给CGRectMake

     var currentNodeName:String!

    func parser(parser: NSXMLParser, didStartElement elementName: String!, namespaceURI: String!, qualifiedName qName: String!, attributes attributeDict: [NSObject : AnyObject]!)
    {

        currentNodeName = elementName

        if elementName=="background"
        {

            let stringx = attributeDict["x"]! as String
            //if let n = NSNumberFormatter().numberFromString(stringx)
            //{
             //   let x = CGFloat(n)
            //}
            //var y = attributeDict["y"]! as CGFloat
            //var width = attributeDict["width"]! as CGFloat
           var Height = attributeDict["Height"]! as String
            backgroundRect = CGRectMake(0,0,0,0)

        }

    }

从xml里面读出来都是string,怎么转换呢?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • shiter 2015-07-07 07:32
    已采纳

    自己找到解决办法了,

     let x = CGFloat(((attributeDict["x"]! as String )as NSString).floatValue)
                    let y = CGFloat(((attributeDict["y"]! as String ) as NSString).floatValue)
                    let Width = CGFloat(((attributeDict["Width"]! as String )as NSString).floatValue)
                    let Height = CGFloat(((attributeDict["Height"]! as String ) as NSString).floatValue)
    
                    m_backgroundRect = CGRectMake(x,y,Width,Height)
    
    
    已采纳该答案
    打赏 评论
  • sun_ljz 2015-06-29 11:22
     let floatValue = Float(stringValue)
    
    打赏 评论

相关推荐 更多相似问题