菜鸟教程小白 发表于 2022-12-13 07:58:55

iOS将方法名称添加到NSDictionary并随后调用它


                                            <p><p>我正在尝试这件事,但似乎无法正确处理。我有一个名为 <strong>dataDictionary</strong> 的 <code>NSMutableDictionary</code> 与 <strong>2 KeyValue</strong> 对, <code>Id</code> 和 <code>MethodName</code></p>

<pre><code>NSMutableDictionary *dataDictionary = [ initWithCapacity:1];
;
;
</code></pre>

<p>虽然我的方法是</p>

<pre><code>-(void)myMethod{ … }
</code></pre>

<p>所以我需要做的是获取方法名称 <code>;</code> 并调用该方法... </p>

<p>任何帮助都会很棒..提前感谢</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用 <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSelectorFromString" rel="noreferrer noopener nofollow">NSSelectorFromString</a> 将其存储为 <code>NSString</code>或 <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSStringFromSelector" rel="noreferrer noopener nofollow">NSStringFromSelector</a> </p>

<p>或者像一个<code>NSValue</code></p>

<pre><code>NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                     , @&#34;foo&#34;,
                     nil];

SEL aSel = [ pointerValue];
</code></pre>

<p>更多信息:
<a href="https://stackoverflow.com/questions/2175818/add-method-selector-into-a-dictionary" rel="noreferrer noopener nofollow">Add method selector into a dictionary</a> ,
<a href="https://stackoverflow.com/questions/2822602/store-selector-as-value-in-an-nsdictionary" rel="noreferrer noopener nofollow">Store selector as value in an NSDictionary</a> </p></p>
                                   
                                                <p style="font-size: 20px;">关于iOS将方法名称添加到NSDictionary并随后调用它,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23904098/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23904098/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS将方法名称添加到NSDictionary并随后调用它