GVKun编程网logo

swift – 带扩展名的自动类型转换:这里发生了什么?(swift扩展 存储属性)

1

对于想了解swift–带扩展名的自动类型转换:这里发生了什么?的读者,本文将是一篇不可错过的文章,我们将详细介绍swift扩展存储属性,并且为您提供关于(char)(i+'a')JAVA:这里发生了什

对于想了解swift – 带扩展名的自动类型转换:这里发生了什么?的读者,本文将是一篇不可错过的文章,我们将详细介绍swift扩展 存储属性,并且为您提供关于(char) (i + 'a') JAVA:这里发生了什么?、Autohotkey 并不总是发送密钥? 这里发生了什么、AWESOME SWIFT-swift.libhunt.com-swift类库网站、c – 这里发生了什么?的有价值信息。

本文目录一览:

swift – 带扩展名的自动类型转换:这里发生了什么?(swift扩展 存储属性)

swift – 带扩展名的自动类型转换:这里发生了什么?(swift扩展 存储属性)

我将阅读The Swift Programming Language一书的第一章,我正处于描述扩展关键字的部分.

我去了“实验”:

“Write an extension for the Double type that adds an absoluteValue property.”

我得到它像这样工作:

extension Double {
    var absoluteValue: Double {
        if(self < 0) {
            return self * -1
        }

        return self
    }
}

(-10.5).absoluteValue    // 10.5

但它似乎也适用于整数:

(-4).absoluteValue       // 4.0

这里发生了什么?编译器是否将类型从Int更改为Double,因为它看到Double上有一个absoluteValue扩展而不是Int?

这似乎是这种情况,因为如果我在Int上添加同名的另一个扩展名,如下所示:

extension Int {
    var absoluteValue: Int {
        return 42
    }
}

这会覆盖Double上的扩展名.并且(-4).absoluteValue返回42

有没有办法添加一个仅适用于双打但不适用于Ints的扩展程序?

编辑:看起来它正在编译时进行转换,因为我没有为我的文字定义一个类型,所以它转换了它.以下产生错误

var i:Int = -4;
i.absoluteValue

“Playground execution Failed: error: :12:1: error: ‘Int’ does not have a member named ‘absoluteValue’
i.absoluteValue
^ ~~~~~~~~~~~~~”

编辑2:它似乎只适用于文字;以下也会产生错误:

var i = -4;
i.absoluteValue

解决方法

是的,你写的扩展实际上只适用于双打,而不适用于Ints.看看这个例子:

extension Double {
    var absoluteValue: Double {
        if (self < 0) {
            return self * -1
        }

        return self
    }
}

var double: Int = 10
double.absoluteValue // Int does not have a member named absoluteValue

但是,在您的代码中,编译器会隐式地将Int转换为Double.

(char) (i + 'a') JAVA:这里发生了什么?

(char) (i + 'a') JAVA:这里发生了什么?

如何解决(char) (i + ''a'') JAVA:这里发生了什么?

我遇到了一段类似的代码:

public class print {
    public static void main(String[] args) {
        for (int i = 0; i < 6; i++) {
            System.out.print((char) (i + ''a''));
        }
    }
}

如果我运行它,我会得到“abcdef”。

我的问题是关于这个表达式:(char) (i + ''a'')

我有点直观地了解正在发生的事情,但我想要一个关于计算机如何翻译它的严格的逐步解释。如某些答案所示,字符只是一个显示为字符的数字。很好,但是这个带括号的语法实际上是做什么的?是转换吗?我也可以将它用于其他类型吗?

解决方法

Java char 是 16 位整数类型。 ''a''97 相同,您可以在 System.out.println((int) ''a''); 中看到它 - 因此 98''b'',以此类推在整个 ASCII 表中。

,

a 的 ASCII value 是 97

When i = 0,i + ''a'' = 0 + 97 => When cast into char,it will be a
When i = 1,i + ''a'' = 1 + 97 => When cast into char,it will be b
When i = 2,i + ''a'' = 2 + 97 => When cast into char,it will be c
...and so on
,

您可以将 char 用作 integer 值,反之亦然

以下代码可能有帮助:

char aChar = ''a''; // a char
int aCharAscii = aChar; // 97

char bChar = ''a'' + 1; // b char
int bCharAscii = aChar + 1; // 98

Autohotkey 并不总是发送密钥? 这里发生了什么

Autohotkey 并不总是发送密钥? 这里发生了什么

如何解决Autohotkey 并不总是发送密钥? 这里发生了什么

基本上我不明白发生了什么,任何澄清都会有帮助

我正在尝试在我的程序中设置热键“_”和“alt _”。所以我制作了一个自动热键脚本,用“_”替换“/”,用“alt _”替换其他新闻,但它似乎并不总是在我的程序中工作? (除非我向命令发送垃圾邮件)

我的脚本:

/::
Send _ ;if I keep clicking "/" I always get the desired effect
return

但是当我打开我的程序并按“/”时,它不会启动指定的场景,除非我这样做(但是它总是在文本中用 _ 替换“/”。

/::
Send_ ; This code works fine,I don''t need to keep clicking "/" it works the first time but the codes awful
Send_
Send_
Send_
Send_
return

当我使用上述代码时,streamlabs OBS 程序每次都会选取它并切换到适当的场景。但是当我只使用一个 Send _ 时,它有时会立即起作用,有时我需要多次按下它。

我的完整代码(看起来工作正常)如下:

+/:: ; This is the hotkey I press "Shift /" to start the script
toggler := !toggler ; this changes a boolean every time the script is run
if (toggler) { ; Every odd run this happens (hides scene)
    Send +-
    Send +-
    Send +-
    Send +- ;; I feel like all these lines are redundant
    Send +-
    Send +-
    Send +-
    Send +-
    Send +-
    Send +-         
} else { ; every even run this happens (shows scene)
    Send !+-
    Send !+-
    Send !+-
    Send !+- ;; again idk why I need all these lines instead of just 1
    Send !+-
    Send !+-
    Send !+-
    Send !+-
    Send !+-
    Send !+-
}
return

基本上,streamlab obs 不允许您使用相同的热键打开/关闭场景,这就是我所做的,但我无法忍受我的解决方案:(

AWESOME SWIFT-swift.libhunt.com-swift类库网站

AWESOME SWIFT-swift.libhunt.com-swift类库网站

https://swift.libhunt.com/categories/688-events

29 Events libraries and projects

  • ORDERED BY POPULARITY
  • ORDER BY DEV ACTIVITY
  •  ReactiveCocoa

      10.0   7.3  Objective-C
    ReactiveCocoa (RAC) is a Cocoa framework inspired by Functional Reactive Programming. It provides APIs for composing and transforming streams of values over time.
  •  RxSwift

      9.9   8.8 L3  Swift
    Microsoft Reactive Extensions (Rx) for Swift and iOS/OSX platform.
  •  PromiseKit

      9.8   8.7 L5  Swift
    async promise programming lib.
  •  ReSwift

      9.6   6.3 L5  Swift
    Unidirectional Data Flow in Swift
  •  Bond

      9.3   8.8 L1  Swift
    a Swift binding framework.
  •  BrightFutures

      8.3   4.7 L4  Swift
    promise and future lib for swift.
  •  Katana

      8.2  8.7 L4  Swift
    Swift apps a la React and Redux.
  •  ReactorKit

      7.8   6.4  Swift
    A framework for reactive and unidirectional application architecture.
  •  ReactKit

      7.4   0.0 L3  Swift
    Swift Reactive Programming.
  •  FutureKit

      6.4  0.7 L2  Swift
    A Swift based Future/Promises Library.
  •  SwiftEventBus

      6.4   3.2 L5  Swift
    A publish/subscribe event bus optimized for iOS.
  •  EmitterKit

      5.7  3.5 L5  Swift
    an implementation of event emitters and listeners in swift.
  •  Signals

      4.9   3.3 L5  Swift
    replaces delegates and notifications.
  •  Safe

      4.9   0.0 L2  Swift
    A modern concurrency and synchronization for Swift.
  •  snail

      4.5   7.1 L5  Swift
    An observables framework for Swift
  •  Reactor

      4.1   2.4 L5  Swift
    Powering your RAC architecture.
  •  VueFlux

      3.8  6.8  Swift
    Unidirectional Data Flow State Management Architecture
  •  SignalKit

      3.7   0.0 L5  Swift
    Swift event and binding framework.
  •  Observable

      3.7   6.2  Swift
    The easiest way to observe values.
  •  When

      3.4   5.4 L5  Swift
    A lightweight implementation of Promises in Swift.
  •  Caravel

      3.3   0.0 L2  Swift
    A Swift event bus for UIWebView and JS.
  •  Future

      2.5   0.0 L4  Swift
    A micro framework providing Future.
  •  NoticeObserveKit

      2.3   0.0 L5  Swift
    NoticeObserveKit is type-safe NotificationCenter wrapper that associates notice type with info type.
  •  Aftermath

      1.8   0.0 L5  Swift
    Stateless message-driven micro-framework in Swift.
  •  Notificationz

      1.6   2.5 L5  Swift
    Helping you own NSNotificationCenter by providing a simple, customizable adapter.
  •  Forbind

      1.2   0.0 L4  Swift
    Functional chaining and Promises in Swift.
  •  ReduxSwift

      1.0   0.0 L5  Swift
    Predictable state container for Swift apps too
  •  PureFutures

      0.7   0.0 L4  Swift
    Futures and Promises library.
  •  SSEventFlow

      0.3  4.4 L5  Swift
    A type safe alternative to NSNotification, inspired by Flux.

c – 这里发生了什么?

c – 这里发生了什么?

这不编译,

#include <boost/intrusive_ptr.hpp>

class X
{
public:
 void intrusive_ptr_add_ref(X* blah)
 {
 }

void intrusive_ptr_release(X * blah)
{
}

};



int main()
{
  boost::intrusive_ptr<X> ex(new X);
}

但这样做:

#include <boost/intrusive_ptr.hpp>

class X
{
public:
  friend void intrusive_ptr_add_ref(X* blah)
  {
  }

  friend void intrusive_ptr_release(X * blah)
  {
  }

};



int main()
{
  boost::intrusive_ptr<X> ex(new X);
}

还有这个 :

#include <boost/intrusive_ptr.hpp>

    class X
    {
    public:


    };


    void intrusive_ptr_add_ref(X* blah)
      {
      }

      void intrusive_ptr_release(X * blah)
      {
      }

int main()
{
  boost::intrusive_ptr<X> ex(new X);
}

我想这与SFINAE有关(我还没有想过要理解)? friend限定符是否将已定义的函数作为自由函数放在封闭的命名空间中?

编辑

谁删除了他们的帖子,成员函数非朋友作为add_ref和release(documention中没有提到这些特定的成员函数……)确实解决了问题.使用好友限定符的嵌套定义会发生什么?

解决方法

从boost :: intrusive_ptr的文档:

Every new intrusive_ptr instance increments the reference count by using an unqualified call to the function intrusive_ptr_add_ref,passing it the pointer as an argument. Similarly,when an intrusive_ptr is destroyed,it calls intrusive_ptr_release; this function is responsible for destroying the object when its reference count drops to zero. The user is expected to provide suitable deFinitions of these two functions. On compilers that support argument-dependent lookup,intrusive_ptr_add_ref and intrusive_ptr_release should be defined in the namespace that corresponds to their parameter; otherwise,the deFinitions need to go in namespace boost.

这意味着intrusive_ptr_add_ref和intrusive_ptr_release不应该是成员函数,而是自由函数(友元函数就像这样).此外,它们在没有限定条件的情况下被调用,因此它们应该位于全局命名空间或ADL找到的某个位置.

编辑:关于使用朋友限定符的嵌套定义的问题:友元函数被定义为非成员函数,因此朋友void intrusive_ptr_add_ref(X * blah)将被称为intrusive_ptr_add_ref(my_x_ptr)而不是my_x_ptr-> intrusive_ptr_add_ref().

今天关于swift – 带扩展名的自动类型转换:这里发生了什么?swift扩展 存储属性的分享就到这里,希望大家有所收获,若想了解更多关于(char) (i + 'a') JAVA:这里发生了什么?、Autohotkey 并不总是发送密钥? 这里发生了什么、AWESOME SWIFT-swift.libhunt.com-swift类库网站、c – 这里发生了什么?等相关知识,可以在本站进行查询。

本文标签: