限制textField中的字符和字符长度(_:shouldChangeCharactersInRange:replacementString :)(Limiting characters and character length in textField(_:shouldChangeCharactersInRange:replacementString:))
我的应用中有一个用户名textField,我想限制为24个字符,不允许“|” 用户名中的管道。
我可以使用下面的代码单独执行这些操作,但是我无法将这两者组合到textField(_:shouldChangeCharactersInRange:replacementString :)中。
此代码成功禁止“|” 在用户名字段中。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if textField === usernameField { // Create an `NSCharacterSet` set let set = NSCharacterSet(charactersInString:"|") // At every character in this "set" contained in the string, // split the string up into components which exclude the characters // in this inverse set let components = string.componentsSeparatedByCharactersInSet(set) // Rejoin these components let filtered = components.joinWithSeparator("") // If the original string is equal to the filtered string, i.e. if no // characters were present to be eliminated, the input is valid // and the statement returns true; else it returns false return string == filtered } else { return true } }
此代码成功将用户名字段限制为24个字符。
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // Added to limit title to <= 40 characters guard let text = meetupTitleTextField.text else { return true } let newLength = text.utf16.count + string.utf16.count - range.length return newLength <= 48 // Bool }
我真的很感激有关如何将这两者结合到textField(_:shouldChangeCharactersInRange:replacementString :)的任何建议
I have a username textField in my app that I want to limit to 24 characters and not allow "|" a pipe in the username.
I am able to do each of these individually with the code below but I have having trouble combining these both into textField(_:shouldChangeCharactersInRange:replacementString:).
This code is successfully disallows a "|" in the username field.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { if textField === usernameField { // Create an `NSCharacterSet` set let set = NSCharacterSet(charactersInString:"|") // At every character in this "set" contained in the string, // split the string up into components which exclude the characters // in this inverse set let components = string.componentsSeparatedByCharactersInSet(set) // Rejoin these components let filtered = components.joinWithSeparator("") // If the original string is equal to the filtered string, i.e. if no // characters were present to be eliminated, the input is valid // and the statement returns true; else it returns false return string == filtered } else { return true } }
This code successfully limits the username field to 24 characters.
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // Added to limit title to <= 40 characters guard let text = meetupTitleTextField.text else { return true } let newLength = text.utf16.count + string.utf16.count - range.length return newLength <= 48 // Bool }
I would really appreciate any advice on how to combine these both into textField(_:shouldChangeCharactersInRange:replacementString:)
原文:https://stackoverflow.com/questions/37366639
最满意答案
放入akka http路由的此代码将输出连续的随机数流,而不会压倒websocket
path("randomNums") { val src = Source.fromIterator(() => Iterator.continually(ThreadLocalRandom.current.nextInt())) .filter(i => i > 0 && i % 2 == 0).map(i => TextMessage(i.toString)) extractUpgradeToWebsocket { upgrade => complete(upgrade.handleMessagesWithSinkSource(Sink.ignore, src)) } }
This code put into a akka http route will output a continuous stream of random numbers without overwhelming the websocket
path("randomNums") { val src = Source.fromIterator(() => Iterator.continually(ThreadLocalRandom.current.nextInt())) .filter(i => i > 0 && i % 2 == 0).map(i => TextMessage(i.toString)) extractUpgradeToWebsocket { upgrade => complete(upgrade.handleMessagesWithSinkSource(Sink.ignore, src)) } }
相关问答
更多-
Akka-Http WebSocket:Akka流使用WebSocket作为接收器(Akka-Http WebSocket: Akka stream use WebSocket as a sink)[2023-12-30]
放入akka http路由的此代码将输出连续的随机数流,而不会压倒websocket path("randomNums") { val src = Source.fromIterator(() => Iterator.continually(ThreadLocalRandom.current.nextInt())) .filter(i => i > 0 && i % 2 == 0).map(i => TextMessage(i.toString)) extractU ... -
我已经使用akka文档中的websocket服务器测试了您的客户端实现,但我没有收到任何连接错误。 您的websocket客户端连接成功。 这就是为什么我猜测问题出在你的服务器实现上。 object WebSocketServer extends App { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() import Directives._ val greeterWe ...
-
你的fromWebsocket流返回流,而不是消息,你的actorSink接收消息。 如果你想要它编译,你必须确保它返回消息,例如你可以将map更改为flatMapConcat 。 这将确保从流中返回的消息在新的消息流中连接。 Your fromWebsocket flow returns streams, not messages, and your actorSink takes messages. If you want it to compile you have to make sure it r ...
-
WebSocket确实由两个独立的流组成,只是那些流(可能)不在同一个JVM上。 您有两个对等通信,一个是服务器,另一个是客户端,但从已建立的WebSocket连接的角度来看,差异无关紧要。 一个数据流是对等体1向对等体2发送消息,另一个流体是对等体2向对等体1发送消息,然后在这两个对等体之间存在网络边界。 如果您一次查看一个对等体,则对等体1从对等体2接收消息,而在第二流体对等体1中向对等体2发送消息。 每个对等体都有一个接收部分的接收器和一个发送部分的源。 实际上你确实有两个Source和两个Sinks ...
-
在Akka Http中,Websocket和普通的路由可以相同吗?(Can Websocket and normal get route be same in Akka Http?)[2021-08-04]
下面的内容应该做的事情 val route = path("hello") { optionalHeaderValueByType[UpgradeToWebSocket](()) { case Some(upgrade) => complete(upgrade.handleMessages(flow)) case None => get { complete("Simple hello") } } } Something along the lines of t ... -
尝试使用recoverWithRetries组合器( 此处为 docs)。 这允许您提供管道将切换到的备用Source ,以防上游失败。 在最简单的情况下,您可以重新使用相同的Source ,它应该发出新的连接。 val wsSource = source via webSocketFlow wsSource .recoverWithRetries(attempts = -1, {case e: Throwable => wsSource}) .to(sink) 注意 attempts = -1 ...
-
如果您想将整个HttpRequest发送到您的接收器,我会说最简单的方法是使用alsoTo组合器。 结果将是符合的 val mySink: Sink[HttpRequest, NotUsed] = ??? val handlerFlow = Flow[HttpRequest].alsoTo(mySink).via(RouteResult.route2HandlerFlow(route)) val bindingFuture = Http().bindAndHandle(handlerFlow, "loc ...
-
不.WebSocket是双向通道,因此Akka-HTTP将其建模为Flow 。 如果在您的特定情况下您只关心通道的一侧,则可以通过使用Flow.fromSinkAndSource(Sink.ignore, mySource)或Flow.fromSinkAndSource(mySink, Source.maybe)由您来形成具有“静音”侧的Flow.fromSinkAndSource(mySink, Source.maybe) ,视情况而定。 根据文件 : 将根据空闲超时设置删除非活动WebSocket连接。 ...
-
Websocket连接终止被建模为常规流完成,因此在您的情况下,您可以使用Sink.foreach产生的物化Future[Done] : val flow = Flow.fromSinkAndSourceMat(printSink, source)(Keep.both) val (upgradeResponse, (sinkClose, sourceClose)) = Http().singleWebSocketRequest(..., flow) sinkClose.onComplete { ...
-
在Flow处理Future , mapAsync通常是您正在寻找的。 要添加到您的特定示例: def asyncOp(msg: TextMessage): Future[SomeReply] = ??? def tailorResponse(r: SomeReply): TextMessage = ??? def wsFlow: Flow[Message, Message, Any] = Flow[Message] .mapConcat {...} .mapAsync(parallelis ...