How to add String Extension on Apple DocC - ios

I am checking new Apple documentation on Xcode 13 beta. I am able to build documentation with DocC.
But I am not able to see any documentation created for extensions like String extension, Date extension.
As per example for below case it will not add any documentation.
extension String {
/// Description for DocC
func myMethod() {
}
}
Consider, If I have created an extension for any struct or class, then it is adding documentation like below code
public struct MyStruct {
}
extension MyStruct {
/// Description
func myMethod() {
}
}
Is this default behaviour by apple or I am missing anything?

This is not currently supported. DocC cannot generate documentation for anything but a single module's types. Extensions to types outside your module aren't handled. (It also won't allow you to link to types outside your module using ``...`` references.)

Related

Implemented iOS AppIntents don't show in Shortcuts

I'm trying to test out the new AppIntents API that is currently on iOS16 Beta. Looking at the documentation, the implementation seems pretty straight forward, but after implementing it, I'm not seeing my AppIntent in the Shortcuts app. The device is running iOS 16.0.
This is how I implemented the AppIntent:
import AppIntents
struct DoSomethingIntent: AppIntent {
static var title: LocalizedStringResource = "This will do something"
static var description = IntentDescription("Does something")
func perform() async throws -> some PerformResult {
return .finished(value: "Done")
}
}
According to the documentation the Shortcuts app should be able to find my AppIntent after my app gets installed, but I see that's not the case. Does anybody know what my implementation is missing?
AppIntents APIs are a little bit new and have strange behaviours and the documentation is very poor.
In my case, I was able to make them work by adding the \(.applicationName) parameter to the phrase. Try this:
struct LibraryAppShorcuts: AppShortcutsProvider {
#AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
AppShortcut(intent: DoSomethingIntent(), phrases: ["Do something with \(.applicationName)"])
}
}
First you need to select your Xcode-beta.app via xcode-select
Clean derived data
Kill your app & shortcut app
Add a shortcut library in your code
struct LibraryAppShorcuts: AppShortcutsProvider {
#AppShortcutsBuilder static var appShortcuts: [AppShortcut] {
AppShortcut(intent: DoSomethingIntent(), phrases: ["My something phrase"])
}
}
Build

Not able to use Kotlin Extension Function written in common in iOS as Swift Extension

I have a Kotlin Multiplatform project setup with Android & cocoapods for iOS.
I have a file Extensions.kt in commonMain/src with the following function:
fun String.isValidEmail(): Boolean {
return validate(ValidatorRegex.EMAIL)
}
I am able to access this function in Android as a String extension:
"abcd#gmail.com".isValidEmail()
But in iOS using Swift, I need to call it as a static method of another class:
ExtensionsKt.isValidEmail("abcd#gmail.com")
It should convert that commonMain/src method to a Swift extension of String instead of a class with a static method.
Am I missing any configuration?
You're doing everything right here. Unfortunately, this option is not available for now. Extensions conversion may be performed correctly for some classes, but Swift's String is not the one. This is a known inconvenience, and K/N team is working hard to make it better.

What does this function do "override func `self`() -> Self"?

I am reading AWS SDK sample code on GitHub and saw these following two functions:
// MARK: NSObjectProtocol hack
override func isEqual(object: AnyObject?) -> Bool {
return super.isEqual(object)
}
override func `self`() -> Self {
return self
}
This is only one comment above "NSObjectProtocol hack", which doesn't really make sense to me. Could anyone explain what they are trying to do here?
This is a legacy code you can ignore when using the latest Swift. The earlier versions of Swift had issues recognizing that your mapper object indirectly inherits from NSObject and implements NSObjectProtocol. The current version of Swift does not have the issue, so you can safely ignore these two methods.
This is an example of using a keyword as an identifier. You have to place backtick marks before and after the keyword to use it as such. According to Apple's documentation (under the Identifiers section)
To use a reserved word as an identifier, put a backtick (`) before and after it. For example, class is not a valid identifier, but `class` is valid. The backticks are not considered part of the identifier; `x` and x have the same meaning.
The // MARK: NSObjectProtocol hack is used for commenting a section of code that allows Xcode to format the list of properties/methods etc. into groups.

Create iOS framework using swift

I am trying to create an iOS framework using swift. I follow this blog and also few others but the output is not the way I want.
Below is my original source file
public class TestClass: NSObject {
// publicVar description
public var publicVar: Int = 0
// doSomething description
public func doSomething() {
print("doSomething method")
}
}
After adding the framework into my project it's create an TestFramwork-Swift.h
You can see it's not contain my description. I want the framework
header files like Apple.
Can anyone help me to figure out this. I am not able to understand where I am doing wrong. I am also not able to add more swift files into my framework.
Just use triple slash instead of double slash for comments you want to show in your framework headers.

Cannot declare a public protocol extension with internal requirements

I am programming a media player app and created my own framework for managing all the player functionality. In this framework I have a public protocol called PlayerControllerType and an internal protocol _PlayerControllerType. In PlayerControllerType I have declared all the methods and properties, which should be accessible from outside the framework. In _PlayerControllerType I have defined a couple of properties, which are used by the concrete types implementing PlayerControllerType inside the framework. One of these types is PlayerController. Its declaration is as follows:
public class PlayerController<Item: Equatable>: NSObject, PlayerControllerType,
_PlayerControllerType, QueueDelegate
Now I want to provide a couple of default implementations for the classes in my framework, which conform to PlayerControllerType and the internal _PlayerControllerType, for example:
import Foundation
import MediaPlayer
public extension PlayerControllerType where Self: _PlayerControllerType, Item == MPMediaItem, Self.QueueT == Queue<Item>, Self: QueueDelegate {
public func setQueue(query query: MPMediaQuery) {
queue.items = query.items ?? []
}
}
This works as expected in Xcode 7 Beta 4. Yesterday I updated to Beta 6 and got this error:
"Extensions cannot be declared public because its generic requirement uses an internal type" (also see screenshot).
I find this error irritating. Of course no type outside of my framework benefits of this extension because it cannot access the internal protocol _PlayerControllerType, but it is very useful for the types inside my framework which implement both PlayerControllerType and _PlayerControllerType.
Is this just a bug in the Swift compiler or is this the intended behavior?
It's is pretty unfortunate that this doesn't work anymore because now I have to put these methods into a newly created base class for all my PlayerControllers.
Any help or feedback would greatly appreciated.
Kai
EDIT:
Here is a shortened example of the protocols and their extensions:
public protocol PlayerControllerType {
typealias Item
var nowPlayingItem: Item {get}
func play()
}
protocol _PlayerControllerType {
var nowPlayingItem: Item {get set}
}
public extension PlayerControllerType where Self: _PlayerControllerType {
/*
I want to provide a default implementation of play() for
all my PlayerControllers in my framework (there is more than one).
This method needs to be declared public, because it implements a
requirement of the public protocol PlayerControllerType.
But it cannot be implemented here, because this extension
has the requirement _PlayerControllerType. It needs this requirement,
because otherwise it cannot set the nowPlayingItem. I don't want to
expose the setter of nowPlayingItem.
I could make a base class for all PlayerControllers, but then I'm
restricted to this base class because Swift does not support
multiple inheritance.
*/
public func play() {
if nowPlayingItem == nil {
nowPlayingItem = queue.first
}
// Other stuff
}
}
You need to declare an access level of the _PlayerControllerType protocol as 'public'.
public protocol _PlayerControllerType {
// some code
}
According to (The Swift Programming Language - Access Control),
A public members cannot be defined as having an internal or private type, because the type might not be available everywhere that the
public variable is used. Classes are declared as internal by default,
so you have to add the public keyword to make them public.
A member (class/protocol/function/variable) cannot have a higher
access level than its parameter types and return type, because the
function could be used in situations where its constituent types are
not available to the surrounding code.

Resources