最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

cfstring - Question about CFArrayGetValueAtIndex in Swift - Stack Overflow

programmeradmin0浏览0评论

I called this to get an array of attribute names of an AXUIElement element.

    var attrNames: CFArray? = nil;
    AXUIElementCopyAttributeNames(element, &attrNames);

My understanding is that attrNames is a CFArray of CFString.

I then call:

var foo = CFArrayGetValueAtIndex(attrNames!, idx)

In Objective-C, this corresponding call returns an NSArray* of CFString*. How do I access the individual strings of this array in Swift?

I tried this: let attrName : NSString = (NSString) CFArrayGetValueAtIndex(attrNames!, index);

but it won't compile: Cannot convert value of type '(NSString).Type' to specified type 'NSString'

I also tried CFString instead of NSString, but got a similar message.

I called this to get an array of attribute names of an AXUIElement element.

    var attrNames: CFArray? = nil;
    AXUIElementCopyAttributeNames(element, &attrNames);

My understanding is that attrNames is a CFArray of CFString.

I then call:

var foo = CFArrayGetValueAtIndex(attrNames!, idx)

In Objective-C, this corresponding call returns an NSArray* of CFString*. How do I access the individual strings of this array in Swift?

I tried this: let attrName : NSString = (NSString) CFArrayGetValueAtIndex(attrNames!, index);

but it won't compile: Cannot convert value of type '(NSString).Type' to specified type 'NSString'

I also tried CFString instead of NSString, but got a similar message.

Share Improve this question asked 2 days ago Deron JohnsonDeron Johnson 31 silver badge1 bronze badge New contributor Deron Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2
  • attrNames is a CFArray of CFString, you need to safely extract values in Swift. – K.pen Commented 2 days ago
  • (NSString) value is how you do a cast in C and Objective C, but not Swift. In Swift, it's spelt as, as? or as!, depending on the kind of conversion, and what you want to happen if that value doesn't have the type you expected. – Alexander Commented 2 days ago
Add a comment  | 

1 Answer 1

Reset to default 1

Don't use CFArray at all; it isn't Swift. Cast the CFArray safely to a Swift array of String, and then talk Swift, using subscripting to index the array as usual. Thus for example:

var attrNames: CFArray?
AXUIElementCopyAttributeNames(element, &attrNames)
guard let attrNames = attrNames as? [String] else { return }
var foo = attrNames[idx]
发布评论

评论列表(0)

  1. 暂无评论