Get NSImage from NSTextField in Swift - swift

I used to retrieve the NSImage in a subclass of NSTextField from Obj-C like this:
NSDictionary *attributedVal = [[self attributedStringValue] attributesAtIndex:i effectiveRange:&effectiveRange];
if ([[attributedVal allKeys] containsObject:NSAttachmentAttributeName]) {
NSTextAttachment *attachment = [attributedVal valueForKey:NSAttachmentAttributeName];
NSCell *attachmentCell = (NSCell *)[attachment attachmentCell];
... [[attachmentCell image] name] ...
}
When I try to do the same in Swift I can't seem to be able to cast attachmentCell but get a compiler error:
let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange)
if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment {
let attachmentCell = attachment.attachmentCell as NSCell // does not work
...
}

Thanks to Nate Cook. The following works:
let attributedVal = attributedStringValue.attributesAtIndex(i, effectiveRange: effectiveRange)
if let attachment = attributedVal[NSAttachmentAttributeName] as? NSTextAttachment {
let attachmentCell = attachment.attachmentCell as NSTextAttachmentCell
let image = attachmentCell.image
...
}

Related

Can't cast to NSMutableString in Swift

I have Swift struct like this:
if(isSearching == true){
let contactDict :NSDictionary = self.filteredArray?.object(at: indexPath.row) as! NSDictionary;
let strArray :NSArray = contactDict.object(forKey: kName) as! NSArray
nameString = strArray.componentsJoined(by: "") as! NSMutableString
//nameString = (contactDict.object(forKey: kName) as? String as! NSMutableString)
companyNameString = (contactDict.object(forKey: kCompanyName) as AnyObject).object(at: 0) as? NSString;
designationString = (contactDict.object(forKey: kDesignation) as AnyObject).object(at: 0) as? NSString;
profileImage = contactDict.object(forKey: kProfilePic) as? UIImage;
connectStatus = contactDict.value(forKey: kLinkStatus) as? NSString;
if(profileImage?.accessibilityIdentifier == "Img_placeholder"){
profileImage = nil;
}
The error showing like this :
Could not cast value of type 'NSTaggedPointerString' (0x1b5b89900) to 'NSMutableString' (0x1b5b959c0)
How can i solve this issue?
If you need nameString to be a NSMutableString, then try this:
nameString: NSMutableString = strArray.componentsJoined(by:"").mutableCopy()
The error comes from
nameString = strArray.componentsJoined(by: "") as! NSMutableString
because you can't downcast here. NSTaggedPointerString is a (private) subclass of NSString but not of NSMutableString. You should create a new mutable string instead:
nameString = NSMutableString(string: strArray.componentsJoined(by: ""))
But as #Sweeper said in the comments it should be better to use Swift strings.

Issue with file manager in swift 3.1

I have the below code that worked fine, up until the release of swift 3.1.
func loadImage() {
id = userPhotoModel.id
let fileManager = FileManager.default
let imagePath = (self.getDirectoryPath() as NSString).appendingPathComponent(photoName)
if fileManager.fileExists(atPath: imagePath){
let imageFromPath = resizeImage(named: (contentsOfFile: imagePath))
print("name of photo retrieved: \(photoName)")
self.userPhoto.image = imageFromPath
}else{
print("No Image")
}
}
Now, swift 3.1 wants to add as! String to:
let imageFromPath = resizeImage(named: (contentsOfFile: imagePath) as! String)
However, when I run the app, it crashes in this location with no error message as per the below image.
What is causing this?
EDIT: Here is the resizeImage func
fileprivate func resizeImage(named name: String) -> UIImage
{
var image = UIImage(named: name)
if image!.size.height > image!.size.width
{
self.userPhoto.contentMode = .scaleAspectFit
}
else
{
image = image!.resizeTo(self.userPhoto.bounds)
}
return image!
}
The issue is the confusing syntax in the line:
let imageFromPath = resizeImage(named: (contentsOfFile: imagePath))
That should simply be:
let imageFromPath = resizeImage(named: imagePath)
No cast required and correct in any Swift 3.x.

Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails

This line let userInfo = notification.userInfo as! NSDictionary I get a warning: Cast from '[NSObject : AnyObject]?' to unrelated type 'NSDictionary' always fails
I try to use let userInfo = notification.userInfo as! Dictionary<NSObject: AnyObject> replace let userInfo = notification.userInfo as! NSDictionary. But I get an error :Expected '>' to complete generic argument list. How to fix the warning.
Xcode 7.1 OS X Yosemite
This is my code:
func keyboardWillShow(notification: NSNotification) {
let userInfo = notification.userInfo as! NSDictionary //warning
let keyboardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardBoundsRect = self.view.convertRect(keyboardBounds, toView: nil)
let keyboardInputViewFrame = self.finishView!.frame
let deltaY = keyboardBoundsRect.size.height
let animations: (()->Void) = {
self.finishView?.transform = CGAffineTransformMakeTranslation(0, -deltaY)
}
if duration > 0 {
} else {
animations()
}
}
NSNotification's userInfo property is already defined as a(n optional) dictionary.
So you don't need to cast it at all, just unwrap it.
func keyboardWillShow(notification: NSNotification) {
if let userInfo = notification.userInfo {
...
}
}
all the rest of your code should work as is.
You are trying to force cast an optional to an NSDictionary. Try:
let userInfo = notification.userInfo! as NSDictionary
This worked for me.

Swift - Why can I not return an NSNumber or Double data type from this NSDictionary object?

The second line of the code segment below returns an error unless I change the portion that reads "as NSNumber" to "as String". The value returned in rowData["lngID"] is a numeric value. Can someone please explain this to me?
let rowData: NSDictionary = objReport as NSDictionary
let lngReportID = rowData["lngID"] as NSNumber
What I'm actually attempting to do here is take a JSON response and load it into an array of objects as follows. Perhaps there is a better way to achieve this. Any suggestions for a better approach is much appreciated. First, the function didReceiveAPIResults returns the results to the app. Then the function loadReportsIntoArray is called.
func loadReportsIntoArray(pReports: NSArray) {
arrayPoints = []
for (intRow, objReport) in enumerate(pReports) {
// index is the index within the array
// participant is the real object contained in the array
let rowData: NSDictionary = objReport as NSDictionary
let lngReportID = rowData["lngID"] as NSNumber
let lngReportTypeID = rowData["lngTypeID"] as NSNumber
let strOtherTypeName = rowData["strOtherTypeName"] as String
let strDescription = rowData["strDescription"] as String
let dtmFirstReport = rowData["dtmFirstReport"] as String
let dblLat = rowData["dblLat"] as NSNumber
let dblLong = rowData["dblLong"] as NSNumber
let strReportedByUsername = rowData["strReportedByUsername"] as String
let lngReportedByID = rowData["lngReportedBy"] as NSNumber
let lngCommentCount = rowData["lngCommentCount"] as NSNumber
let lngNumLikes = rowData["lngNumLikes"] as NSNumber
let blnUserLikedEvent = rowData["blnUserLikedEvent"] as Bool
var objReport = Report(plngReportID: lngReportID, plngReportTypeID: lngReportTypeID, pstrOtherTypeName: strOtherTypeName, pstrDescription: strDescription, pdtmFirstReport: dtmFirstReport, pdblLat: dblLat, pdblLong: dblLong, pstrReportedByUsername: strReportedByUsername, plngReportedByID: lngReportedByID, plngCommentCount: lngCommentCount, plngNumLikes: lngNumLikes, pblnUserLikedEvent: blnUserLikedEvent)
//arrayPoints.append(objReport)
}
}
func didReceiveAPIResults(results: NSDictionary) {
var success: NSInteger = results["success"] as NSInteger
if success == 1 {
var resultsArr = results["geopoints"] as NSArray
dispatch_async(dispatch_get_main_queue(), {
self.loadReportsIntoArray(resultsArr)
})
}
else {
// Error occurred
}
}
I was able to recreate your error using the following code:
let objReport = NSDictionary(object: "string", forKey: "lngID")
let rowData: NSDictionary = objReport as NSDictionary
let lngReportID = rowData["lngID"] as NSNumber // Error
However, changing the objReport to NSDictionary(object: NSNumber(integer: 0), forKey: "lngID") solved the problem. Therefore, I think your problem is the object stored for the key lngID isn't an NSNumber.
For the solution to this you should look at Kumar Nitin's answer to check you've got a number stored, or you could use the code, they both do the same thing pretty much:
if let lngID = rowData["lngID"] as? NSNumber {
// Do stuff with lngID.
}
In swift, you don't have NSNumber, however you can use the Obj C's NSNumber if need be.
The above code for NSNumber should be as follows if you are expecting a double or float or int. Add a check to ensure the value is not nil, or else it will crash your app.
if let lngReportID = rowData["lngID"] as? Int {
//Do the task required
}

'AnyObject?' does not have a member named 'count' compiler error

First of all, I tried using JSON Serialization in the following code and I tried looping through each items available in the array. However, it must be type casting that I am missing something. Here is the snippet of what I am trying to do:
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:
NSJSONReadingOptions.MutableContainers, error: nil) as
NSDictionary
var items = [[String:String]()]
var item: AnyObject
var authorDictionary: AnyObject
for var i = 0; i < jsonResult["items"].count; i++ {
items.append([String:String]())
items[i]["content"] = item["content"] as? NSString
items[i]["title"] = item["title"] as? NSString
items[i]["publishedDate"] = item["published"] as? NSString
authorDictionary = item["author"] as NSDictionary
items[i]["author"] = item["displayName"] as? NSString
}
To resolve your error, change this:
jsonResult["items"]
to this:
(jsonResult["items"] as! NSArray)
Sorry, i am not with my xcode here, but looking the code i think in something about this.
let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options:
NSJSONReadingOptions.MutableContainers, error: nil) as
NSDictionary
var items = [[String:String]()]
var item: AnyObject
var authorDictionary: AnyObject
if let itemsFromJson:NSArray = jsonResult["items"] as? NSArray
{
for itemJson in itemsFromJson {
items.append([String:String]())
items[i]["content"] = itemJson["content"] as? NSString
items[i]["title"] = itemJson["title"] as? NSString
items[i]["publishedDate"] = itemJson["published"] as? NSString
authorDictionary = itemJson["author"] as NSDictionary
items[i]["author"] = itemJson["displayName"] as? NSString
}
}