ios - unexpectedly found nil while unwrapping an Optional value from NSMutableString -


I can not get around this error when I run this function do I get a fatal error. Fatal error: unexpectedly, while the bottom is an optional value unwrapping minus the function:

  function changeSelectedFontColor (FONTCOLOR: UIColor) {go selectedRange: NSRange = self.textView.selectedRange bridesmaid currentAttributesDict: NSDictionary = TextView .textStorage.attributesAtIndex (selectedRange.location, effectiveRange: zero) var currentFont: UIFont = currentAttributesDict.objectForKey (NSFontAttributeName) to italFont = as UIFont [NSFontAttributeName: UIFont (name: "Georgia italics" size: 18.0)] bridesmaid currentColor: UIColor = currentAttributesDict.objectForKey (NSForegroundColorAttributeName) as UIColor bridesmaid currentBGColor: UIColor = currentAttributesDict.objectForKey (NSBackgroundColo RAttributeName) as CurrentUnderlinedText on UIColor: UIFont = currentAttributesDict.objectForKey (NSUnderlineStyleAttributeName) UIFont as currentparagraphStyle: NSMutableParagraphStyle = CurrentAttributesDict.objectForKey (NSParagraphStyleAttribu TeName) as NSMutableParagraphStyle}  

all lines like this:

  var currentFont: UIFont = currentAttributesDict.objectForKey (NSFontAttributeName) UIFont As  

You can extract a value from a dictionary and insert a clear (to UIFont in this case) any of the following linesCan not fail for the following reasons:

  • The key specified in the dictionary is not present, hence the objectForKey returns zero , and the artist fails
  • Value exists, but it is not the type of person you are casting it, so the artist fails

How do I not all these variables , So I can not provide the correct answer that best solves your problem.

However, you can get rid of runtime exception by using an alternate cast, with right? . Note that this will turn all expression results into an alternative, for example, in the line of the code above, the current font UIFont? will be type:

  var currentFont UIFont? = Current Entities Dict As an ObjectForreake (NSFOntOtube)? UIFont  

What you have to do with all these optional variables is: You can use alternate bindings (but I expect nested code to be large number of ) Or, if possible, use them as alternatives.


Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -