Cannot invoke 'subscript' with an argument list of type '(string: NSString, attribute


This question already has an answer here:

<ul><li> <a href="/questions/26499815/nsfontattributedstring-worked-before-xcode-6-1" dir="ltr" rel="nofollow">NSFontAttributedString worked before XCode 6.1</a> <span class="question-originals-answer-count"> 2 answers </span> </li> </ul>

I have the following code but when I compile I'm getting this error:


"Cannot invoke 'subscript' with an argument list of type '(string: NSString, attributes: [NSString : UIFont?])".


This code is working fine on <strong>xCode 6.0.1</strong> but after after upgrading to <strong>6.1</strong> it is giving this error.

let textboldFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Bold", size: 15.0)] let textregularFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Regular", size: 15.0)] let para = NSMutableAttributedString() let attributedstring1 = NSAttributedString(string: dateArray[1] as NSString, attributes:textboldFont)


Unfortunately the error messages from Swift are sometimes not really helpful. The problem is not the subscript, it's the attributes array.

As you can see in the header the UIFont initializer you use returns an optional UIFont:

init?(name fontName: String, size fontSize: CGFloat) -> UIFont

But NSAttributedString initializer expects an [NSObject : AnyObject] array. Note the AnyObject, it's not AnyObject?. So you have to unwrap the UIFont first.

You have two options:

The safe way. Check if those UIFonts could be created, otherwise use system supplied font:

let textboldFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Bold", size: 15.0) ?? UIFont.boldSystemFontOfSize(15.0)] let textregularFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Regular", size: 15.0) ?? UIFont.systemFontOfSize(15.0)]

The dangerous way. Forcefully unwrap the optional fonts. This will crash if the font could not be created:

let textboldFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Bold", size: 15.0)!] let textregularFont = [NSFontAttributeName:UIFont(name: "ProximaNova-Regular", size: 15.0)!]


  • UIAlertController and UIAlertControllerStyleActionSheet customization
  • Can't get attributed string to work in Swift
  • Create Vertical lines between Uilabels inside of a stack view [duplicate]
  • How to use NSWritingDirectionAttributeName in swift?
  • How do I know if text exceeds the bounds of a text view?
  • Resizing UITableViewCells in iOS4 versus iOS3
  • How to stop the horizontal scrolling programmatically?
  • Android studio 3.1 rebuild project each time to see the changes
  • Does data used by an app get deleted when a user installs an update?
  • Hadoop/map-reduce: Total time spent by all maps in occupied slots vs. Total time spent by all map ta
  • Easy Way to Get Averages Based on Names in List
  • Google Bigquery Command Line Return Limit
  • if(!isset($_POST[“user”]) ignored and returns Undefined Index
  • Upgrade project from WorkLight 6.1 to MobileFirst 7.1
  • R sqldf renaming a field in a select statement
  • Available space left on drive - WinAPI - Windows CE
  • extjs4 catching the scroll event on panel
  • chrome video src change not working
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Repository Browser Only - \"Repository moved permanently to… please relocate”
  • CSS bleed-through with cfinput type=“datefield”
  • Group list of tuples by item
  • SAXReader not re-ecape characters
  • Cannot upload to OneDrive using the new SDK
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • How can I set a binding to a Combox in a UserControl?
  • Lost migrations and Azure database is now out of sync
  • Get one-time binding to work for ng-if
  • Dynamically accessing properties of knockoutjs observable array
  • Jenkins: How To Build multiple projects from a TFS repository?
  • swift auto completion not working in Xcode6-Beta
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • How can I estimate amount of memory left with calling System.gc()?
  • Hits per day in Google Big Query
  • -fvisibility=hidden not passed by compiler for Debug builds
  • File not found error Google Drive API
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Converting MP3 duration time